凝望长空 发表于 2012-02-19 19:45

Ruby笔记五(简单的字符串操作

Ruby笔记五(简单的字符串操作)#E8.1-2.rb 字符串操作
#双引号括起来的字符串会有转义,例如:“\n” 表示换行。还有一些其它的转义符号,比如制表符之类。
str = "this is you string .\n"
puts str*2
=begin result:
this is you string .
this is you string .
=end
#单引号括起来的字符串并不会对字符串作任何解释
str2 = 'this is you string .\n '
puts str2*2 #this is you string .\n this is you string .\n

=begin 字符串内嵌表达式
在双引号扩起来的字符串中,不仅可以使用各种转义符,
而且可以放置任意的Ruby表达式在 #{ } 之中,
这些表达式在使用这个字符串的时候被计算出值,然后放入字符串
=end
def hello(name)
" Welcome, #{name} !"
end

puts hello("kaichuan") # Welcome, kaichuan !
puts hello("Ben") # Welcome, Ben !
#匹配一个正则表达式,用“=~”
str="Hello,Jeriffe,Welcome!"
puts str =~ /Jeriffe/ #6
puts str =~ /f/ #10
puts str =~ /ABC/ #nil

#不匹配一个正则表达式,用“!~”
puts str !~ /Jeriffe/ # false
puts str !~ /f/ # false

puts str !~ /ABC/ # true

复制代码

小鬼萌萌控 发表于 2012-02-19 19:46

谢谢分享
页: [1]
查看完整版本: Ruby笔记五(简单的字符串操作