1)、self[substr]
當自身當中包含substr的時候。則生成並返回一致的字符串
irb(main):075:0> substr = "bar"
=> "bar"
irb(main):076:0> result = "foobar"[substr]
=> "bar"
irb(main):077:0> p result
"bar"
=> "bar"
irb(main):078:0> substr.eql?(result)
=> true
2)、capitalize、 capitalize!
將字符串的首字符轉換爲大寫,其餘的轉換爲小寫
irb(main):095:0> xx = "abcdef"
=> "abcdef"
irb(main):096:0> xx
=> "abcdef"
irb(main):097:0> xx.capitalize
=> "Abcdef"
irb(main):098:0> xx
=> "abcdef"
irb(main):099:0> xx.capitailze!
irb(main):102:0> xx
=> "Abcdef"
這裏順便說明一下方法尾部帶!跟不帶!的區別,其實通過上邊的例子已經很清楚的看到了。帶!的是直接對對字符串內容進行修改,而不帶!的只是對字符串進行規定的操作,並不對字符串本身造成破壞。
3)、casecmp(other)
此方法跟 “<=>”方法類似,但是它忽略大小寫
irb(main):105:0> "A".casecmp("a")
=> 0
irb(main):106:0> "A"<=>"a"
=> -1
4)、center、 ljust、 rjust、 center(length, str)、 ljust(length, str)、 rjust(length, str)
irb(main):107:0> "foo".center(20)
=> " foo "
irb(main):108:0> "foo".center(20,"*")
=> "********foo*********"
irb(main):109:0> "foo".ljust(20)
=> "foo "
irb(main):110:0> "foo".ljust(20,"*")
=> "foo*****************"
irb(main):111:0> "foo".rjust(20)
=> " foo"
irb(main):112:0> "foo".rjust(20,"*")
=> "*****************foo"
5)、count
該方法返回在該字符串中str所含字符出現的次數
irb(main):125:0> xx
=> "abc"
irb(main):126:0> "abcc".count("c")
=> 2
irb(main):127:0> "abcc".count("a-c")
=> 4
irb(main):128:0> "123345657688".count("23456")
=> 8
以上方法中如果“-”出現在中間就表示一個範圍,當`^'出現在頭部時表示"取反"
以下是該方法的一個應用場景
n_line = File.open("foo").read.count("\n") #=>統計文件的行數(文件尾部要有換行符)
6)、crypt
生成並返回一個由self和salt加密而成的字符串,salt一般是使用數字、字母、.以及下劃線等構成的字符串,一般應選擇儘量隨機的字符串
salt = [rand(64),rand(64)].pack("C*").tr("\x00-\x3f","A-Za-z0-9./")
passwd.crypt(salt).
該方法一般很難利用加密後的字符串求出原字符串,所以一般用於用戶密碼加密
7)、delete(str1,str2, ...)、 delete!(str1,str2, ...)
從該字符串中刪除str所包含的字符。若出現多個參數,則組多個參數的交集
irb(main):129:0> "123454656546789".delete("2-8","^4-6")
=> "14546565469"
irb(main):130:0> "234545667".delete("345")
=> "2667"
改方法會返回修改後的字符串,而delete!會修改原字符串。
8)、each_line、 each_byte
對字符串中的各行進行迭代,對字符串的各個字符進行迭代
“abcdefgh”.each_byte {|e| print e, " "} #=>97 98 99 100 101 102 103 104 => "abcdefgh"
8)、gsub(pattern, replace)、 gsub(pattern) {|matched| .... }
當帶有replace參數的時候,原字符串就通過匹配pattern,然後將匹配的字符用replace替換。但是當不帶replace參數的時候就可以當一個迭代器使用,這個時候pattern就會被當做一個參數被傳遞到改迭代器塊。這個用法比較強大。
irb(main):138:0> "abcabc".gsub(/b/,"B")
=> "aBcaBc"
irb(main):139:0> "abcabc".gsub(/b/) {|e| e.upcase!}
=> "aBcaBc"
同樣,帶有!的會對字符串自身進行修改。
(這個方法還有待再去研究)
9)、intern、 to_sym
以上兩個方法的使用方法基本類似,都是將一個字符串轉化爲一個符號
irb(main):140:0> "abc".intern
=> :abc
irb(main):141:0> "abc".to_sym
=> :abc
irb(main):142:0> "cat and dog".intern
=> :"cat and dog"
10)、next、 next!、 succ、 succ!
返回下一個字符串,是按照26個英文字母的順序或者10進制數的順序返回
irb(main):145:0> "abc".next
=> "abd"
irb(main):146:0> "fg11".succ
=> "fg12"
11)、oct(將字符串看成8進制字符串), hex(將字符串看成是10進制字符串)
12)、squeeze([str[,str2[, ... ]]])
壓縮由str所含字符構成的重複字符串,`a-c'表示從a到c,而像"^0-9"這樣,當`^'出現在頭部時表示"取反",若給出多個參數,則意味着會使用所有參數的交集進行壓縮
irb(main):147:0> "11112345565656".squeeze
=> "1234565656"
irb(main):148:0> "11112345565656".squeeze("1")
=> "12345565656"
13)、strip、 strip!
刪除頭部和尾部的所有空白字符串,空白字符是指" \t\r\n\f\v"
irb(main):149:0> "abc ".strip
=> "abc"
irb(main):150:0> "abc\t\n".strip
=> "abc"
13)、lstrip、lstrip!
刪除字符串頭部的所有空白字符。空白字符是指" \t\r\n\f\v"。
14)、rstrip 、rstrip!
刪除字符串尾部的所有空白字符。空白字符是指" \t\r\n\f\v"。
15)、swapcase、swapcase!
將所有的大寫換成小寫,小寫換成大寫