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數組