判断字符串是否为回文字符串

String objects hold ordered sequences of bytes, typically characters, usually to form pieces of human-readable text. They're a very common object type in all programming languages, and Ruby has a number of high-level and a few low-level ways to create, access and manipulate String objects.

字符串对象保存有序的字节序列 ,通常是字符,通常形成人类可读的文本。 在所有编程语言中,它们都是非常常见的对象类型,并且Ruby具有创建,访问和操作String对象的许多高级和一些低级方法。

Strings are most often created with a String literal. A literal is a special syntax in the Ruby language that creates an object of a specific type. For example, 23 is a literal that creates a ​Fixnum object. As for String literals, there are several forms.

字符串通常是使用String文字创建的。 文字是Ruby语言中的一种特殊语法,可以创建特定类型的对象。 例如,23是创建一个Fixnum对象文字。 至于字符串文字,有几种形式。

单引号和双引号字符串 ( Single-Quotes and Double-Quoted Strings )

Most languages have a String literal similar to this, so this may be familiar. The types of quotes, ' (single quote, apostrophe or hard quote) and " (double quote or soft quote) are used to enclose string literals, anything between them will be turned into String objects. The following example demonstrates this.

大多数语言都具有与此相似的String文字,因此可能很熟悉。 引号的类型使用'(单引号,撇号或硬引号 )和“(双引号或软引号 )来包围字符串文字,它们之间的任何内容都将变成String对象。以下示例对此进行了演示。

But there are some differences between single and double quotes. Double quotes or soft quotes enable some magic to happen behind the scenes. Most useful is interpolation inside strings, useful for inserting the value of a variable into the middle of a string. This is achieved by using the #{ … } sequence. The following example will ask you for your name and greet you, using interpolation to insert your name into the string literal that's printed.

但是单引号和双引号之间存在一些差异。 双引号或软引号使幕后发生一些魔术。 最有用的是在字符串内部进行插值,对于将变量的值插入字符串的中间很有用。 这是通过使用#{…}序列来实现的。 下面的示例将使用插值法将您的姓名插入到打印出的字符串文字中,并询问您的姓名并打招呼。

Note that any code can go inside the braces, not just variable names. Ruby will evaluate that code and whatever is returned it will attempt to insert it into the string. So you could just as easily say "Hello, #{gets.chomp}" and forget about the name variable. However, it's good practice not to put long expressions inside the braces.

请注意,任何代码都可以放在花括号内,而不仅仅是变量名。 Ruby将评估该代码,无论返回什么,它都会尝试将其插入到字符串中。 因此,您可以轻松地说出“ Hello,#{gets.chomp}”,不必理会 name变量。 但是,最好不要在括号中使用长表达式。

Single quotes, apostrophes, or hard quotes are much more restrictive. Inside of the single quotes, Ruby will perform no interpolation or escape sequences other than escaping the single quote character and backslash itself (\' and \\ respectively). If you don't intend to use interpolation, it's recommended to use single quotes more often than not.

单引号,撇号或硬引号更具限制性。 在单引号内部,除了转义单引号字符和反斜杠本身(分别为\'\\ )外,Ruby不会执行任何内插或转义序列 。 如果您不打算使用插值,建议您经常使用单引号。

The following example will attempt to interpolate a variable inside of single quotes.

下面的示例将尝试在单引号内插入变量。

If you run this you'll get no error, but what will be printed?

如果运行此命令,则不会出现任何错误,但是将显示什么内容?

The interpolation sequence was passed through uninterpreted.

插值序列未解释。

什么时候应该使用单引号和双引号 ( When Should I Use Single and Double Quotes )

This is a matter of style. Some prefer to use double quotes all of the time unless they become inconvenient. Others would rather use single quotes unless the interpolation behavior is intended. There's nothing inherently dangerous about using double quotes all of the time, but it does make some code easier to read. You don't need to read a string when reading through code if you know there are no interpolations in it because you know the string itself won't have any side effects. So which string literal form you use is up to you, there is no real right and wrong way here.

这是一个风格问题。 有些人宁愿一直使用双引号,除非它们引起不便。 其他人宁愿使用单引号,除非打算进行插值行为。 始终使用双引号没有本质上的危险 ,但这确实使某些代码更易于阅读。 如果您知道其中没有插值,则无需在读取代码时读取字符串,因为您知道字符串本身不会产生任何副作用。 因此,您使用哪种字符串文字形式取决于您自己,这里没有真正的对与错方法。

转义序列 ( Escape Sequences )

What if, in a string literal, you want to include a quote character? For instance, the string "Steve said "Moo!" won't work. And neither will 'Can't touch this!'. Both of these strings include the quote character inside of the string, effectively ending the string literal and causing a syntax error. You could switch quote characters, like 'Steve said "Moo!"', but that doesn't really solve the problem. Instead, you can escape any quote character inside the string, and it will lose its special meaning (in this case, the special meaning is to close the string).

如果要在字符串文字中包含引号怎么办? 例如,字符串“ Steve说” Moo!”将不起作用, “'Ca n't touch this! 也将不会起作用。 这两个字符串都在字符串中包含引号字符,从而有效地结束了字符串文字并导致语法错误。您可以切换引号字符,例如“ Steve说“ Moo!”” ,但这并不能真正解决问题而是,您可以转义字符串中的任何引号字符,并且它将失去其特殊含义(在这种情况下,特殊含义是关闭字符串)。

To escape a character, prepend it with the backslash character. The backslash character tells Ruby to ignore any special meaning the next character may have. If it's a matching quote character, don't end the string. If it's a hash sign, don't start an interpolation block. The following example demonstrates this use of backslash to escape special characters.

要转义字符,请在其前面加上反斜杠字符。 反斜杠字符告诉Ruby忽略下一个字符可能具有的任何特殊含义。 如果它是匹配的引号字符,则不要以字符串结尾。 如果是哈希符号,请不要启动插值块。 下面的示例演示如何使用反斜杠转义特殊字符。

The backslash character can be used to remove any special meaning from the following character but, confusingly, it can also be used to denote special behavior in double-quoted strings. Most of these special behaviors have to do with inserting characters and byte sequences that cannot be typed or represented visually. Not all Strings are character strings or may contain control sequences intended for the terminal, and not the user. Ruby gives you the ability to insert these types of strings using the backslash escape character.

反斜杠字符可用于从以下字符中删除任何特殊含义,但令人困惑的是,它也可用于表示双引号字符串中的特殊行为。 这些特殊行为中的大多数与插入无法以视觉方式键入或表示的字符和字节序列有关。 并非所有的字符串都是字符串,或者可能不包含针对终端(而非用户)的控制序列。 Ruby使您能够使用反斜杠转义符插入这些类型的字符串。

  • \n - A newline character. The puts method does this automatically, but if you wish to insert one in the middle of a string, or the string is destined for something other than the puts method, you can use this to insert a newline in a string.

    \ n-换行符。 puts方法会自动执行此操作,但是,如果您希望在字符串的中间插入一个,或者该字符串指定用于puts方法以外的其他内容,则可以使用此方法在字符串中插入换行符。

  • \t - A tab character. The tab character moves the cursor over (on most terminals) to a multiple of 8, so this is very useful for display tabular data. However, there are better ways of doing this, and using the tab character is considered a bit archaic or hackish.\ t-制表符。 制表符将光标移到(在大多数终端上)至8的倍数,因此这对于显示表格数据非常有用。 但是,有更好的方法来执行此操作,并且使用制表符被认为有些陈旧或怪异。
  • \nnn - A backslash followed by 3 numbers will denote an ASCII character represented by 3 octal digits. Why octal? Mostly for historical reasons.\ nnn-反斜杠后跟3个数字将表示一个由3个八进制数字表示的ASCII字符。 为什么八进制? 主要是出于历史原因。
  • \xnn - A backslash, an x, and 2 hex digits. The same as the octal version, only with hex digits.\ xnn-反斜杠,x和2个十六进制数字。 与八进制版本相同,仅带有十六进制数字。

You'll probably never use most of these, but know that they exist. And also remember that they only work in double-quoted strings.

您可能永远不会使用其中的大多数,但是知道它们存在。 还要记住,它们只能在双引号字符串中使用。

The next page discusses multi-line strings and an alternate syntax for string literals.

下一页讨论多行字符串和字符串文字的替代语法。

多行字符串 ( Multi-Line Strings )

Most languages don't allow multi-line string literals, but Ruby does. There's no need to end your strings and append more strings for the next line, Ruby handles multi-line string literals just fine with the default syntax.

大多数语言不允许多行字符串文字,但是Ruby允许。 无需结束字符串并为下一行附加更多字符串,Ruby使用默认语法即可处理多行字符串文字。

替代语法 ( Alternate Syntax )

As with most other literals, Ruby provides an alternate syntax for string literals. If you're using a lot of quote characters inside your literals, for example, you may want to use this syntax. When you use this syntax is a matter of style, they're usually not needed for strings.

与大多数其他文字一样,Ruby为字符串文字提供了另一种语法。 例如,如果您在文字中使用很多引号字符,则可能要使用此语法。 当您使用此语法只是样式问题时,字符串通常不需要它们。

To use the alternate syntax, use the following sequence for single-quoted strings %q{ … }. Similarly, use the following syntax for double-quoted strings %Q{ … }. This alternate syntax follows all the same rules as their "normal" cousins. Also, note that you can use any characters you wish instead of braces. If you use a brace, square bracket, angle bracket or parenthesis, then the matching character will end the literal. If you don't want to use matching characters, you can use any other symbol (anything not a letter or number). The literal will be closed with another of the same symbol. The following example shows you several ways to use this syntax.

要使用备用语法,请对单引号字符串%q {…}使用以下序列。 同样,对双引号字符串%Q {…}使用以下语法。 这种替代语法遵循与其“正常”表亲相同的所有规则。 另外,请注意,您可以使用任何希望的字符代替大括号。 如果使用花括号,方括号,尖括号或括号,则匹配字符将以文字结尾。 如果您不想使用匹配的字符,则可以使用任何其他符号(任何字母或数字除外)。 文字将用另一个相同的符号关闭。 以下示例显示了使用此语法的几种方法。

The alternate syntax also works as a multi-line string.

备用语法也可以用作多行字符串。

翻译自: https://www.thoughtco.com/string-literals-2908302

判断字符串是否为回文字符串

判断字符串是否为回文字符串_字符串文字相关推荐

  1. 字符串最长回文子串_最长回文子串

    字符串最长回文子串 Problem statement: 问题陈述: Given a string str, find the longest palindromic substring. A sub ...

  2. 判断字符串是否构成回文_构成字符串回文的最小删除数

    判断字符串是否构成回文 Problem statement: 问题陈述: Given string str find the minimum number of deletions such that ...

  3. python判断字符串是否为回文if语句_如何python判断字符串是否为回文?

    如何python判断字符串是否为回文? python判断字符串是否为回文的方法: 1.回文字符串:一个字符串,不论是从左往右,还是从右往左,字符的顺序都是一样的(如abba,abcba等). 2.如何 ...

  4. python判断字符串是否回文_判断字符串是否为回文 python

    回文正序和逆序一样的字符串,例如abccba 方法一 def is_palindrome1(text): l = list(text) l.reverse() t1 = ''.join(l) if t ...

  5. 【C语言】简单判断字符串是否为回文

    **C语言简单判断字符串是否为回文**哈哈哈哈哈,作为一个刚学c语言不久的小白,今天来分享一下自己今天学到的一个判断字符串是否为回文的解决方法_(:з」∠)_小白用的编译器是Visual Studio ...

  6. python生成回文字符串_回文字符串最长回文子串和子序列 - Python

    Palindrome 回文字符串就是指从前往后和从后往前读,都是一样的,比如"aabcbaa". 注意区分子串和子序列,子串是连续的,子序列可以不连续 题型1:判断字符串是否为回文 ...

  7. 算法笔记-判断链表保存的字符串是否是回文

    <?php/*** 单链表节点** Class SingleLinkedListNode** @package Algo_06*/ class SingleLinkedListNode {/** ...

  8. [YTU]_2803( 判断字符串是否为回文)

    Description 编写程序,判断输入的一个字符串是否为回文.若是则输出"Yes",否则输出"No".所谓回文是指順读和倒读都是一样的字符串. Input ...

  9. 1.7-33编程基础之字符串 33:判断字符串是否为回文

    33:判断字符串是否为回文 查看提交统计提问 总时间限制: 1000ms 内存限制: 65536kB 描述 输入一个字符串,输出该字符串是否回文.回文是指顺读和倒读都一样的字符串. 输入 输入为一行字 ...

最新文章

  1. knn 进行手写数字识别
  2. vbs获取cpu使用率
  3. 消息摘要算法示例(python和go)
  4. C语言再学习 -- 存储类、链接
  5. 从里面学到的关于过去的经验 中篇
  6. SpringBoot与JPA
  7. 【Elasticsearch】Resizing Elasticsearch shards for fun and profit
  8. 痛苦如此持久,像蜗牛充满耐心地移动;快乐如此短暂,像兔子的尾巴掠过秋天的草原...
  9. Windows 键盘快捷键概述
  10. mac上捕获Dock截图的方法
  11. 第二十一讲 ASP.NET页面框架
  12. java insert 返回主键_MyBatis中insert操作返回主键的实现方法 – java – www.cfei.net
  13. matlab空间杜宾模型命令,空间计量模型,包括空间滞后模型、空间误差模型、空间杜宾模型的matlab代码...
  14. 使用 emeditor 删除空行
  15. 前端性能优化(图片优化)
  16. 转载 Latex各种命令、符号、公式、数学符号、排版(非常详细)
  17. 一番谈话,深自反思。
  18. 项目学习 —— 图书商城后台管理
  19. jav中级面试题概览
  20. [转载] 信息系统项目管理师视频教程——29 信息系统监理

热门文章

  1. 福建计算机等级考试分值,福建省考行测分值分布
  2. C语言和Java的区别?
  3. oracle查询当前数据库所有表名
  4. 【JavaScript设计模式】观察者模式
  5. 8、ByteBuf与ByteBuffer的区别
  6. 斐波那契数列递归思路
  7. 红海云如何用产品与口碑引爆HR数字化软件市场
  8. Django的学习日记
  9. 利用onedrive 来同步zotero 文献附件数据
  10. 图片切换效果(滑动门技术)