Ruby字符串

Ruby字符串对象保存并操纵任意字节序列,通常用于表示字符。 它们是使用String:::new创建或作为字面量。

引用

Ruby字符串文字包含在单引号和双引号内。

示例:

#!/usr/bin/ruby   

puts 'Hello everyone'   
puts "Hello everyone"

执行上面代码,得到以下结果 -

F:\worksp\ruby>ruby string-quotes.rb
Hello everyone
Hello everyone
这是一个字符串

F:\worksp\ruby>

访问字符串元素

在方括号[]的帮助下,可以访问Ruby字符串中不同部分的元素。在方括号内写入索引或字符串。

示例:

#!/usr/bin/ruby   

msg = "This tutorial is from xuhuhu.com."   

puts msg["zaixian"]   
puts msg["tutorial"]   

puts msg[0]   

puts msg[0, 2]   
puts msg[0..19]   
puts msg[0, msg.length]   
puts msg[-3]

执行上面代码,得到以下结果 -

F:\worksp\ruby>ruby string-elements.rb
zaixian
tutorial
T
Th
This tutorial is fro
This tutorial is from xuhuhu.com.
o

F:\worksp\ruby>

多行字符串

在Ruby语言中编写多行字符串非常简单。下面是显示打印多行字符串的三种方式。

  • 字符串可以写在双引号内。
  • 使用字符,字符串包含在/字符内。
  • heredoc语法中,使用<<并且字符串包含在单词STRING内。

示例代码:

#!/usr/bin/ruby   
puts "   
A   
AB   
ABC   
ABCD"   

puts %/   
A   
AB   
ABC   
ABCD/   

puts <<STRING
A   
AB   
ABC   
ABCD   
STRING

执行上面代码,得到以下结果 -

F:\worksp\ruby>ruby string-multiline.rb

A
AB
ABC
ABCD

A
AB
ABC
ABCD
A
AB
ABC
ABCD

F:\worksp\ruby>

变量插值

Ruby变量插值是使用字符串文字中的值替换变量。变量名称放在字符串文字中的#{}字符之间。

示例代码:

#!/usr/bin/ruby   

country = "China"   
capital = "Beijing"   

puts "#{capital} is the capital of #{country}."

执行上面代码,得到以下结果 -

F:\worksp\ruby>ruby string-variable-interpo.rb
Beijing is the capital of China.

F:\worksp\ruby>

连接字符串

Ruby连接字符串是要使用多个字符串来创建一个字符串。可以通过连接多个字符串来形成单个字符串。

将Ruby字符串连接成单个字符串有四种方法:

  • 在每个字符串之间使用加(+)号。
  • 在每个字符串之间使用单个空格。
  • 在每个字符串之间使用<<符号。
  • 在字符串之间使用concat方法。

示例

#!/usr/bin/ruby   

string = "This is Ruby Tutorial" + " from zaixian." + " Wish you all good luck."   
puts string   

string = "This is Ruby Tutorial" " from zaixian." " Wish you all good luck."   
puts string   

string = "This is Ruby Tutorial" << " from zaixian." << " Wish you all good luck."   
puts string   

string = "This is Ruby Tutorial".concat(" from zaixian.").concat(" Wish you all good luck.")   
puts string

执行上面代码,得到以下结果 -

F:\worksp\ruby>ruby string-concatenating.rb
This is Ruby Tutorial from zaixian. Wish you all good luck.
This is Ruby Tutorial from zaixian. Wish you all good luck.
This is Ruby Tutorial from zaixian. Wish you all good luck.
This is Ruby Tutorial from zaixian. Wish you all good luck.

F:\worksp\ruby>

冻结字符串

在大多数编程语言中,字符串是不可变的。现有的字符串不能修改,只能创建一个新的字符串。

在Ruby中,默认的字符串是可变的。为了使它们不可修改,可以使用freeze方法。

示例

#!/usr/bin/ruby   

str = "Original string"   
str << " is modified "   
str << "is again modified"   

puts str   

str.freeze   

## 试着去修改看看
str << "is again modified"   

puts str

#str << "And here modification will be failed after using freeze method"

执行上面代码,得到以下结果 -

F:\worksp\ruby>ruby string-freezing.rb
Original string is modified is again modified
Original string is modified is again modified

F:\worksp\ruby>ruby string-freezing.rb
Original string is modified is again modified
string-freezing.rb:11:in `<main>': can't modify frozen String (RuntimeError)

F:\worksp\ruby>

比较字符串

Ruby字符串可以使用以下三个运算符进行比较:

  • 使用==运算符:返回truefalse
  • 使用 eql? 运算符:返回truefalse
  • 使用casecmp方法:如果匹配则返回0,如果不匹配则返回1

示例

#!/usr/bin/ruby

puts "abc" == "abc"   
puts "as ab" == "ab ab"   
puts "23" == "32"   

puts "ttt".eql? "ttt"   
puts "12".eql? "12"   

puts "Java".casecmp "Java"   
puts "Java".casecmp "java"   
puts "Java".casecmp "ja"

执行上面代码,得到以下结果 -

F:\worksp\ruby>ruby string-comparing.rb
true
false
false
true
true
0
0
1

F:\worksp\ruby>

上一篇: Ruby模块 下一篇: Ruby数组