Ruby笔记五(简单的字符串操作)
2012-01-31 16:05
176 查看
#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
#双引号括起来的字符串会有转义,例如:“\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
相关文章推荐
- 2.求一个整数有几位(简单字符串操作)
- leetcode--最长公共前缀--简单的字符串操作
- OC中字符串的简单操作
- 简单基础操作,字符串操作,循环等
- php基础之字符串简单操作
- 【C/C++学院】0826-文件重定向/键盘输入流/屏幕输出流/字符串输入输出/文件读写简单操作/字符文件读写二进制与文本差别/get与getline挖掘数据/二进制与文本差别/随机位置/多线程初级
- shell中字符串的简单操作
- 2.求一个整数有几位(简单字符串操作)
- Python中字符串反转的一个简单操作
- VI 查找字符串 复制 剪切 粘帖 等简单操作
- Educational Codeforces Round 1 B. Queries on a String(字符串简单操作)
- 2.求一个整数有几位(简单字符串操作)
- JAVA中字符串的简单操作
- python反转字符串(简单方法)及简单的文件操作示例
- Qt编程11:QTextStream文件输入输出以及字符串操作的简单使用
- 2.求一个整数有几位(简单字符串操作)
- C++字符串的操作(简单全面)
- 字符串的简单操作
- 字符串的简单操作
- 2.求一个整数有几位(简单字符串操作)