String processing functions
1. Return the length of the string
str.length => integer
str.include? other_str => true .include? .include? .include? ?h
3. String insertion:
str.insert(index, other_str) =>.insert(0, ) .insert(3, ) .insert(4, ) .insert(-3, -3, ) .insert(-1, )
4. String separation. The default Delimiter is space.
str.split(pattern=$;, [limit]) =>.split .split(%r{,\s*}) .split(//) .split(//, 3) .split(%r{\s*}) .split() .split() .split(, 4)
5. String replacement
str.gsub(pattern, replacement) =>|match| block } =>.gsub(/[aeiou]/, ) .gsub(/([aeiou])/, ) .gsub(/./) {|s| s[0].to_s + }
str.replace(other_str) =>= s.replace
str.delete([other_str]+) =>.delete , .delete .delete , .delete
7. Remove leading and trailing Spaces
str.lstrip =>.lstrip .lstrip
8. String Matching
str.match(pattern) => matchdata nil
9. String Inversion
str.reverse =>.reverse
10. Remove duplicate characters
str.squeeze([other_str]*) =>.squeeze .squeeze( ) .squeeze()
11. Convert to numbers
str.to_i=>.to_i
Differences between chomp and chop:
Chomp: Remove \ n or \ r at the end of the string.
Chop: removes the last character at the end of the string, whether it is \ n \ r or a common character.
.chomp .chomp .chomp .chomp .chomp .chomp() .chop .chop .chop .chop
Reprinted from: http://blog.163.com/ma95221@126/blog/static/2482210220100159515220/
Author: Civil Engineering (http://www.cnblogs.com/hongfei)
Address: http://www.cnblogs.com/hongfei/p/3688198.html