ruby str操作
length函数,返回integer
test_str="test string"
test_str.length =>return 11 (返回字符串长度11)
操作实例
1.9.3-p551 :033 > test_str="test string"
=> "test string"
1.9.3-p551 :034 > test_str.length
=> 11
include? 函数,表示字符串是否包含另外一个字符串,返回boolean
test_str="test string"
test_str.include? "st" => return true
操作实例
1.9.3-p551 :035 > test_str.include? "st"
=> true
1.9.3-p551 :036 > test_str.include?"st"
=> true
1.9.3-p551 :038 > test_str.include?" ing" #(因ing前面有空格所以返回false)
=> false
insert函数,表示字符串插入,返回新的字符串
test_str="test string"
test_str.insert(0,"aaa") => return "aaatest_string"
操作实例
1.9.3-p551 :049 > test_str
=> "test string"
1.9.3-p551 :050 > puts test_str
test string
=> nil
1.9.3-p551 :051 > test_str.insert(0,"aaa")
=> "aaatest string"
1.9.3-p551 :052 > test_str.insert(0,"aaa")
=> "aaaaaatest string"
1.9.3-p551 :053 > test_str.insert(-1,"aaa")
=> "aaaaaatest stringaaa"
split 字符串分割,默认分隔符为空格
str.split(pattern=$;, [limit]) => an Array
操作实例
1.9.3-p551 :055 > test_str.split().class
=> Array
1.9.3-p551 :056 > test_str.split(/a/)
=> ["", "", "", "", "", "", "test string"]
1.9.3-p551 :057 > test_str.split(//)
=> ["a", "a", "a", "a", "a", "a", "t", "e", "s", "t", " ", "s", "t", "r", "i", "n", "g", "a", "a", "a"]
1.9.3-p551 :058 >
----------------------------------------------------------------
copy
" now's the time".split #=> ["now's", "the", "time"]
"1, 2.34,56, 7".split(%r{,\s*}) #=> ["1", "2.34", "56", "7"]
"hello".split(//) #=> ["h", "e", "l", "l", "o"]
"hello".split(//, 3) #=> ["h", "e", "llo"]
"hi mom".split(%r{\s*}) #=> ["h", "i", "m", "o", "m"]
"mellow yellow".split("ello") #=> ["m", "w y", "w"]
"1,2,,3,4,,".split(',') #=> ["1", "2", "", "3", "4"]
"1,2,,3,4,,".split(',', 4) #=> ["1", "2", "", "3,4,,"]
-----------------------------------------------------------------
gsub函数 表示字符串替换 返回新的字符串
str.gsub(pattern, replacement) => new_str
str.gsub(pattern) {|match| block } => new_str
"hello".gsub(/[aeiou]/, '*') #=> "h*ll*" #将元音替换成*号
"hello".gsub(/([aeiou])/, '<\1>') #=> "h<e>ll<o>" #将元音加上尖括号,\1表示保留原有字符???
操作实例
替换:
1.9.3-p551 :023 > a = "caofengzhen"
=> "caofengzhen"
1.9.3-p551 :024 > a.gsub("en","an")
=> "caofangzhan"
1.9.3-p551 :025 > a
=> "caofengzhen"
1.9.3-p551 :026 > a.gsub!("en","an")
=> "caofangzhan"
1.9.3-p551 :027 > a
=> "caofangzhan"
1.9.3-p551 :028 > a.gsub!(/an/,"en")
=> "caofengzhen"
1.9.3-p551 :029 > a
=> "caofengzhen"
匹配次数
1.9.3-p551 :030 >a ="caofengzhen caofengzhen caofengzhen"
1.9.3-p551 :034 > a.gsub("en") {|a| puts a}
en
en
en
en
en
en
=> "caofgzh caofgzh caofgzh"
1.9.3-p551 :035 > a.gsub("en") {|c| puts a}
caofengzhen caofengzhen caofengzhen
caofengzhen caofengzhen caofengzhen
caofengzhen caofengzhen caofengzhen
caofengzhen caofengzhen caofengzhen
caofengzhen caofengzhen caofengzhen
caofengzhen caofengzhen caofengzhen
=> "caofgzh caofgzh caofgzh"
delete函数 表示字符串删除
str.delete([other_str]+) => new_str
操作实例
1.9.3-p551 :059 > a.delete "ca"
=> "ofgzh"
1.9.3-p551 :060 > a.delete "ca","zh"
=> "caofgzh"
1.9.3-p551 :061 > a.delete "ca","gz"
=> "caofgzh"
1.9.3-p551 :062 > "hello".delete "l","lo"
=> "heo"
1.9.3-p551 :063 > "hello".delete "l"
=> "heo"
1.9.3-p551 :064 > "hello".delete "aeiou", "^e"
=> "hell"
1.9.3-p551 :065 > "hello".delete "aeiou"
=> "hll"
1.9.3-p551 :066 > "hello".delete "aeioul"
=> "h"
1.9.3-p551 :067 > "hello".delete "haeioul"
=> ""
1.9.3-p551 :068 >
strip、lstrip、rstrip函数,去掉左右空格、左边空格、右边空格
str.lstrip => new_str
操作实例
1.9.3-p551 :001 > " caofengzhen ".strip()
=> "caofengzhen"
1.9.3-p551 :002 > " caofengzhen ".lstrip()
=> "caofengzhen "
1.9.3-p551 :003 > " caofengzhen ".rstrip()
=> " caofengzhen"
1.9.3-p551 :004 >
match函数 字符串匹配
str.match(pattern) => matchdata or nil
操作实例
1.9.3-p551 :016 > " caofengzhen ".match("en")
=> #<MatchData "en">
1.9.3-p551 :017 > " caofengzhen ".match("en")
=> #<MatchData "en">
1.9.3-p551 :018 > " caofengzhen ".match("en")
=> #<MatchData "en">
reverse函数 字符串反转
str.reverse => new_str
操作实例
1.9.3-p551 :022 > " caofengzhen ".reverse
=> " nehzgnefoac "
1.9.3-p551 :023 > " caofengzhen ".reverse()
=> " nehzgnefoac "
to_i\to_s 字符串数字转化
操作实例
1.9.3-p551 :028 > "123A".to_i
=> 123
1.9.3-p551 :029 > "123A4".to_i
=> 123
1.9.3-p551 :030 > "123".to_i
=> 123
1.9.3-p551 :031 > 123.to_s
=> "123"
chomp和chop的区别:
chomp:去掉字符串末尾的\n或\r
chop:去掉字符串末尾的最后一个字符,不管是\n\r还是普通字符
"hello".chomp #=> "hello"
"hello\n".chomp #=> "hello"
"hello\r\n".chomp #=> "hello"
"hello\n\r".chomp #=> "hello\n"
"hello\r".chomp #=> "hello"
"hello".chomp("llo") #=> "he"
"string\r\n".chop #=> "string"
"string\n\r".chop #=> "string\n"
"string\n".chop #=> "string"
"string".chop #=> "strin"
字符串运算操作:
"abc" +"def" => "abcdef"
start_with?函数,表示以"str"开头,返回boolean
end_with? 函数,表示以"str"结尾,返回boolean
操作实例
1.9.3-p551 :032 > "caofengfengzhen".end_with?"en"
=> true
1.9.3-p551 :033 > "caofengfengzhen".start_with?"cao"
=> true
1.9.3-p551 :034 > "caofengfengzhen".start_with?"co"
=> false
浙公网安备 33010602011771号