ruby string
Strings can be formed in ruby using single quotes
or double quotes
.
可以使用single quotes
或double quotes
在ruby中形成字符串。
Any characters such as number or alphabets or symbols enclosed within quotes are Strings
.
用引号括起来的任何字符(例如数字或字母或符号)都是Strings
。
We've already learnt about adding newline character to a string. Likewise, other characters can also be embedded in strings.
我们已经学习了如何在字符串中添加换行符。 同样,其他字符也可以嵌入字符串中。
name = "Bharath\tKumar"
\t
adds tab space\t
添加制表符空间\s
adds space\s
增加空间
For creating strings, this is an added advantage of using double quotes over single quotes. If you use double quotes, you could use single quote as apostrophe
in the string.
对于创建字符串,这是使用双引号而不是单引号的另一个优点。 如果使用双引号,则可以在字符串中使用单引号作为apostrophe
。
str = "Bharath's"
But you cannot use single quote directly inside single quotes.
但是您不能在单引号内直接使用单引号。
But there is a way to do so, using a special character
\
which acts as the escape character, hence telling the compiler/interpreter that the next character has some special meaning, as in case of\n
. In other cases, like for apostrophe, the backslash\
tells the compiler/interpreter that the following character has no special meaning and that the following character is to be part of the string.但是有一种方法可以使用特殊字符
\
充当转义字符,从而告诉编译器/解释器下一个字符具有某些特殊含义,例如\n
。 在其他情况下,例如对于撇号,反斜杠\
告诉编译器/解释器,后面的字符没有特殊含义,并且后面的字符将成为字符串的一部分。
str = 'Bharath\'s'
Use \'
inside double quotation to include apostrophe in string.
在双引号内使用\'
来在字符串中包含撇号。
In the below image, when using a single quote inside the string's single quotes, you can see that we aren't able quit the interactive mode. So, it's not advisable.
在下图中,当在字符串的单引号内使用单引号时,您可以看到我们无法退出交互模式。 因此,这是不明智的。
Ruby:此处的关键字 (Ruby: HERE Keyword)
To create a multiline string and store it in a variable HERE
keyword is used.
要创建多行字符串并将其存储在变量HERE
中,请使用关键字。
This begins with a two less than symbols <<
and the keyword HERE indicates that, until you see the keyword HERE again, store all the values in the variable.
它以少于两个的符号<<
开头,关键字HERE表示在再次看到关键字HERE之前 ,将所有值存储在变量中。
quote = << HERE
What you do
today can
improve
all your
tomorrows
HERE
So, the value What you do\ntoday can\nimprove\nall your\ntomorrows
is stored in the variable quote
. This is the easiest way to store large fragments of text in the variable.
因此, What you do\ntoday can\nimprove\nall your\ntomorrows
值存储在变量quote
。 这是在变量中存储大文本片段的最简单方法。
Ruby:分割方法 (Ruby: Split method)
Split is a useful method for taking the string and splitting into parts. Its syntax is :
拆分是获取字符串并将其拆分成一部分的有用方法。 它的语法是:
<String Value>.split(<DELIMITER>)
Where, the delimiter is the point from where the string specified will be split into two.
其中,定界符是从此处指定的字符串将被拆分为两个的点。
first,last = "Bharath,Kumar".split(/,/)
Here comma (,)
is the delimiter. We use slashes (/)
to delimit(escape) the delimiters.
这里的comma (,)
是分隔符。 我们使用slashes (/)
分隔(转义)定界符。
When this statement is executed, the piece of text before comma is assigned to the variable first and the piece of text after comma is assigned to the variable last.
执行此语句时,逗号前的文本首先分配给变量,而逗号后的文本最后分配给变量。
Therefore in the picture above, value Bharath
is assigned to the variable first
and value Kumar
is assigned to the variable last
.
因此,在上图中,将值Bharath
分配给变量first
,将值Kumar
分配给变量last
。
Ruby:挤压方法 (Ruby: Squeeze method)
Squeeze
is used to remove trailing spaces from the string. Following is the syntax for it :
Squeeze
用于从字符串中删除尾随空格。 以下是它的语法:
<String Value>.squeeze
In the above image, you could note the number of spaces in the variable first. When printing the first variable using squeeze method it removes the trailing spaces.
在上图中,你可以首先注意到的空间在变量的数量。 当使用挤压方法打印第一个变量时,它将删除尾随空格。
ruby string