ruby string

1.改变string的输出格式但不覆盖

a = "Ho! "

a.size#=> 4

a * 3#=> "Ho! Ho! Ho! ""Ho! 

a * 0#=> ""

a.size#=> 4

2.string取值

a="hello there"

a[1]#=> "e"  a[2,3]#=> "llo"

a[2..3]#=> "ll"  a[-3,2]#=> "er"

a[7..-2]#=> "her"  a[-4..-2]#=> "her"  

a[-2..-4]#=> ""  a[11,0]#=> ""  a[11]#=> nil

a[12,0]#=> nil  a[12..-1]#=> nil  

a[/[aeiou](.)\1/]#=> "ell"  

a[/[aeiou](.)\1/,0]#=> "ell"

a[/[aeiou](.)\1/,1]#=> "l"

a[/[aeiou](.)\1/,2]#=> nil

a[/(?

[aeiou])(?[^aeiou])/,"non_vowel"]#=> "l"

a[/(?[aeiou])(?[^aeiou])/,"vowel"]#=> "e"

a["lo"]#=> "lo"  a["bye"]#=> nil

你可能感兴趣的:(ruby string)