ruby string_Ruby String数据类型

ruby string

Strings can be formed in ruby using single quotes or double quotes.

可以使用single quotesdouble quotes在ruby中形成字符串。

Any characters such as number or alphabets or symbols enclosed within quotes are Strings.

用引号括起来的任何字符(例如数字字母符号)都是Strings

String Data Type example in Ruby

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.

在下图中,当在字符串的单引号内使用单引号时,您可以看到我们无法退出交互模式。 因此,这是不明智的。

Including apostrophe in a String by using escape characters in Ruby

Ruby:此处的关键字 (Ruby: HERE Keyword)

To create a multiline string and store it in a variable HERE keyword is used.

要创建多行字符串并将其存储在变量HERE中,请使用关键字。

Here keyword example in Ruby

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.

其中,定界符是从此处指定的字符串将被拆分为两个的点。

Split method in String data type in Ruby
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
Squeeze method in String data type in Ruby

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.

在上图中,你可以首先注意到的空间在变量的数量。 当使用挤压方法打印第一个变量时,它将删除尾随空格。

翻译自: https://www.studytonight.com/ruby/strings-in-ruby

ruby string

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值