#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 

相关文章:

  • 2022-01-29
  • 2021-07-19
  • 2021-08-26
  • 2021-11-27
  • 2022-12-23
  • 2021-09-22
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-02
  • 2022-12-23
  • 2022-12-23
  • 2022-01-25
  • 2022-12-23
相关资源
相似解决方案