Ruby String

Ruby 教學 5 – Ruby String Object 介紹

Ruby String object 是最常用到的 object 之一,常用 “” 來宣告,這篇就來整理並介紹一下 Ruby String object 的特性和常用的 methods 吧!

1. 宣告 Ruby String Object

1.1 String.new

一樣可以用 class.new 的方法 new 出一個 string object instance:

str = String.new("Jimmy")
p str
# "Jimmy"

1.1.1 encoding

encoding 是 String.new 提供的其中一個 argument,用來決定這個 String object 要用什麼方式 encoding,如果 String 沒有任何 argument 的話,預設是用 ASCII-8BIT,但一旦有 argument,則預設用 UTF-8 encoding:

str1 = String.new
str2 = String.new("Jimmy")
p str1.encoding
# #<Encoding:ASCII-8BIT>
p str2.encoding
# #<Encoding:UTF-8>

可以直接在 String.new 後面接上 encoding 來決定編碼方式:

s0 = String.new("你好", encoding: 'UTF-8')
s1 = String.new("你好", encoding: 'ASCII')
p s0
# "你好"
p s1
# "\xE4\xBD\xA0\xE5\xA5\xBD"

1.1.2 capacity

capacity 是 String.new 提供的另一個 argument,用來決定 internal buffer 的 size,會影響到效能,不過 document 中並沒有多作解釋,也沒 google 到相關文章,待我有朝一日修成正果再做更深入的研究,以下是官方 document 舉的例子:

str1 = String.new(capacity: 1)
str2 = String.new(capacity: 1024)
p str1
# ""
p str2
# ""

1.2 “”

可以直接用雙引號來宣告 String object:

str1 = ""
str2 = "Jimmy"
p str1
# ""
p str2
# "Jimmy"

在雙引號中,可以換行,或是使用 String Interpolation 在 String 中插入變數:

age = 25
str = "Jimmy\nis #{age} years old!"

puts str
# Jimmy
# is 25 years old!

1.3 ”

也可以用單引號來宣告 String,和雙引號的差別在於:特殊符號、換行、String Interpolation 都會被視為 String:

age = 25
str = 'Jimmy\nis #{age} years old!'

puts str
# Jimmy\nis #{age} years old!

1.4 MLS

MLS 常用來宣告多行的 String object:

str = <<MLS
Jimmy is 25 years old
live in Taipei
is handsome
MLS

puts str
# Jimmy is 25 years old
# live in Taipei
# is handsome

2. String Instance Methods I – 屬性

我把 String 常用的 methods 分為 5 個部分,第 1 部分比較偏向 object 的 屬性,用來取得這個 String object 相關的資訊。

2.1 String length/size

.length 用來取得字串的長度,和 .size 的作用一樣:

str = "Jimmy"

p str.length
# 5
p str.size
# 5

2.2 String index

.index 可以取得某個字元或是子字串在 String object 中的位置,若沒有給第二個 argument,則回傳第一個符合 argument 的 index,第二個 argument 則是用來指定要從哪個 index 開始找,如果沒有符合 argument 的字串,則回傳 nil:

str1 = "Jimmy is handsome"

p str1.index("i")
# 1
p str1.index("ha")
# 9
p str1.index("H")
# nil
p str1.index("i", 5)
# 6

2.3 String rindex

.rindex 的 r 代表 reverse,一樣會回傳符合 argument 字串的位置,但是是從 String object 的最後一個字元開始找起:

str1 = "Jimmy is handsome"

p str1.rindex("i")
# 6
p str1.rindex("ha")
# 9
p str1.rindex("H")
# nil
p str1.rindex("i", 5)
# 1

2.4 String count

.count 用來計算字元在 String object 中出現的次數,有超過一個字元時,會將次數相加:

str1 = "Jimmy is handsome"

p str1.count("i")
# 2
p str1.count("is")
# 4

2.5 String include?

.include? 用來判斷 String object 是否包含某字串

str = "Jimmy"

p str.include?("J")
# true
p str.include?("Ji")
# true
p str.include?("j")
# false

2.6 String empty?

.empty? 用來判斷這個 String object 是不是空字串:

str1 = ""
str2 = "Jimmy"

p str1.empty?
# true
p str2.empty?
# false

3. String Instance Methods II – 操作 (不改變原 Object)

這一部份整理對 String object 操作後並且回傳 object 的 methods,這些 methods 並不會改變原來的 String object。

3.1 String upcase

.upcase 會將 String object 全部變成大寫後回傳,但不會改變原來的字串。

在 Ruby 中,如果對原來的 object 做一些操作,通常會直接回傳新的 object 而不變更到原本的 object,如果希望直接變更原本的 object,通常這個 method 會有對應的 bang method ( 通常是直接在 method 後面加上! ),bang method 會直接變更原來的 object。
str = "Jimmy"

p str.upcase
# "JIMMY"
p str
# "Jimmy"

str.upcase!
p str
# "JIMMY"

3.2 String downcase

.downcase 和 .upcase 相反,會將字串轉成小寫後回傳:

str = "JIMMY"
p str.downcase
# "jimmy"

3.3 String capitalize

.capitalize 會將 String 變成第一個字大寫,其他小寫:

str1 = "jIMMY"
str2 = "jimmy"
str3 = "jimmy is 25 years old"

p str1.capitalize
# "Jimmy"
p str2.capitalize
# "Jimmy"
p str3.capitalize
# "Jimmy is 25 years old"

3.4 String swapcase

.swapcase 會將 String 的大小寫對調後回傳:

str = "JimMy"
p str.swapcase
# "jIMmY"

3.5 String slice

.slice 可以截取 String object 的部分字串,和直接用 [] 截取一樣:

str1 = "Jimmy is handsome"

p str1.slice(0, 5)
# "Jimmy"
# 從 index = 0 截取 5 個字母
p str1.slice(0..5)
# "Jimmy "
# 從 index = 0 截取到 index = 5 (包含 5)
p str1.slice(0...5)
# "Jimmy"
# 從 index = 0 截取到 index = 5 (不包含 5)


p str1[0, 5]
# "Jimmy"
# 從 index = 0 截取 5 個字母
p str1[0..5]
# "Jimmy "
# 從 index = 0 截取到 index = 5 (包含 5)
p str1[0...5]
# "Jimmy"
# 從 index = 0 截取到 index = 5 (不包含 5)

3.6 String reverse

.reverse 會將字串順序完全顛倒後回傳:

str = "Jimmy"
p str.reverse
# "ymmiJ"

3.7 String squeeze

.squeeze 有兩種執行方式:

  • 沒有任何 argument 時,會刪除所有重複字元
  • 有 argument 時,會刪除字串中重複的 argument
str1 = "JJimmmy iiss handsome"
p str1.squeeze
# "Jimy is handsome"
p str1.squeeze("J")
# "Jimmmy iiss handsome"

3.8 String delete

.delete 會刪除特定字元後回傳:

str1 = "Jimmy is handsome"

p str1.delete("i")
# "Jmmy s handsome"
p str1.delete("isa")
# "Jmmy  hndome"

4. String Instance Methods III – 操作 (會改變原 object)

第二部分主要整理一下會改變原字串的 methods。

4.1 <<

<< 是一個 operator,可以將 << 後面的字串直接加到 << 前面的字串:

str1 = "Jimmy"
str2 = " is handsome"

str1 << str2
p str1
# ""Jimmy is handsome""

如果有超過一個 <<,那只會改變最左邊的字串,其他字串維持不變:

str1 = "Jimmy"
str2 = " is"
str3 = " handsome"

str1 << str2 << str3
p str1
# "Jimmy is handsome"
p str2
# " is"
p str3
# " handsome"

4.2 String concat

.concat 會將 argument 內的 String object 加到 call .concat 的 String object:

str1 = "Jimmy"
str2 = " is handsome"

str1.concat(str2)
p str1
# "Jimmy is handsome"

str1 = "Jimmy"
str2 = " is"
str3 = " handsome"

str1.concat(str2, str3)
p str1
# "Jimmy is handsome"
p str2
# " is"
p str3
# " handsome"

4.3 String prepend

.prepend 和 .concat 類似,只不過 argument 會被加到前方:

str1 = "Jimmy"
str2 = "is "
str3 = "handsome "

str1.prepend(str2)
p str1
# "is Jimmy"

str1 = "Jimmy"
str2 = "is "
str3 = "handsome "

str1.prepend(str2, str3)
p str1
# "is handsome Jimmy"
p str2
# "is "
p str3
# "handsome "

4.4 String insert

.insert 可以指定在 String object 的某個位置插入字串:

str1 = "Jimmy is handsome"
str2 = "tall and "

str1.insert(9, str2)
p str1
# "Jimmy is tall and handsome"

4.5 String clear

.clear 會將 String object 變成空字串:

str1 = "Jimmy is handsome"

str1.clear
p str1
# ""

5. String Instance Methods IV – 轉換成其他 Object

5.1 String split

.spit 可以將 String object 按照特定的字元切割成 Array object:

str1 = "Jimmy is handsome"

p str1.split
# ["Jimmy", "is", "handsome"]
p str1.split(" ")
# ["Jimmy", "is", "handsome"]
p str1.split("is")
# ["Jimmy ", " handsome"]

5.2 String to_i

.to_i 可以將 String object 轉成 Integer object 並回傳,如果

  • String 本身不是數字,則回傳 0
  • String 本身有小數點,無條件捨去後轉成 Integer object 並回傳
str1 = "5"
str2 = "3.14"
str3 = "Jimmy"

p str1.to_i
# 5
p str2.to_i
# 3
p str3.to_i
# 0
p str1.to_i.class
# Integer

5.3 String to_f

.to_f 則是將 String object 轉成 Float object:

str1 = "5"
str2 = "3.14"
str3 = "Jimmy"

p str1.to_f
# 5.0
p str2.to_f
# 3.14
p str3.to_f
# 0.0
p str1.to_f.class
# Float

5.4 String to_sym

.to_sym 可以將 String object 轉換成 Symbol object:

str = "name"

p str.to_sym
# :name

6. String Instance Methods V – Iterate String Object

6.1 String each_char

.each_char 可以遍歷 String object 的所有字元,並對其做操作:

str1 = "Jimmy"

str1.each_char do |c|
  p c + "is"
end
# "Jis"
# "iis"
# "mis"
# "mis"
# "yis"

7. 參考資料

Learn to Code with Ruby
Class: String (Ruby 3.1.2)

如果覺得我的文章有幫助的話,歡迎幫我的粉專按讚哦~謝謝你!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top