转自:http://developer.51cto.com/art/200912/170762.htm
Ruby字符串处理函数总结列表分享
Ruby字符串处理函数包括返回字符串长度函数;判断字符串中是否包含另一个串函数;字符串插入;字符串分隔,默认分隔符为空格等等。
str.length => integer
str.include? other_str
=> true or false
"hello".include? "lo"
#=> true
"hello".include? "ol"
#=> false
"hello".include? ?h
#=> true
Ruby语言对于编程人员来说是一项非常有用的函数。学好这项语言可以帮助我们实现简便轻松的编写方式。下面我们向大家介绍一下Ruby字符串处理函数的一些基本概念。
Ruby字符串处理函数1.返回字符串的长度
Ruby字符串处理函数2.判断字符串中是否包含另一个串
Ruby字符串处理函数3.字符串插入:
str.insert(index, other_str)
=> str "abcd".insert(0, 'X')
#=> "Xabcd" "abcd".insert(3, 'X')
#=> "abcXd" "abcd".insert(4, 'X')
#=> "abcdX" "abcd".insert(-3, 'X') -3, 'X') #=> "abXcd" "abcd".insert(-1, 'X')
#=> "abcdX"
Ruby字符串处理函数4.字符串分隔,默认分隔符为空格
str.split(pattern=$;, [limit])
=> anArray " 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,,".spl