[转帖]ruby的字符串

[转帖]ruby的字符串
原文:http://fujinbing.iteye.com/blog/1126332

%{String} 用于创建一个使用双引号括起来的字符串
%Q{String} 用于创建一个使用双引号括起来的字符串

str=<<END_OF_STRING
a string
END_OF_STRING

%Q!Some String of “Characters”! <==> ” Some String of /”Characters/” “

%q{String} 用于创建一个使用单引号括起来的字符串

%q!Some String of “Characters”! <==> ‘Some String of Characters’

%r{String} 用于创建一个正则表达式字面值

%r{/usr/bin/} <==> ///usr//bin///
%w{String} 用于将一个字符串以空白字符切分成一个字符串数组,进行较少替换
%W{String} 用于将一个字符串以空白字符切分成一个字符串数组,进行较多替换

%W(North South East West) <==> ["North", "South", "East", "West"]

%s{String} 用于生成一个符号对象
%x{String} 用于执行String所代表的命令

%x{ ls /usr/local } <==> `ls /usr/local`

PS:上面几个%表示法中用{}扩住了String,其实这个{} 只是一种分割符,可以换成别的字符,比如(),那么%表示法就是%(String),当然还可以是别的字符,对于非括号类型的分割符,左右两边要相同, 如%!String!

下面我对这些表示法简单举几个例子:

%{String}用于创建一个使用双引号括起来的字符串
这个表示法与%Q{String}完全一样,这边直接句个例子看结果:


1. result = %{hello}
2. puts "result is: #{result}, Type is:#{result.class}"

结果: result is: hello, Type is:String

%Q{String}用于创建一个使用双引号括起来的字符串
%q{String}用于创建一个使用单引号括起来的字符串
从说明中可以看出这两个表示法的区别就是一个使用双引号,一个使用单引号。使用双引号的字符串会对字符串中的变量做较多替换,而单引号则做较少的替换,具 体看例子。先看%Q{String}:

1. world = "world"
2. result = %Q{hello #{world}}
3. puts "result is: #{result}, Type is:#{result.class}"

结果: result is: hello world, Type is:String

换成%q{String}:

1. world = "world"
2. result = %q{hello #{world}}
3. puts "result is: #{result}, Type is:#{result.class}"

结果:
result is: hello #{world}, Type is:String

从上面的结果可以看出,较少替换的情况下,#{world}被解析成了字符串,而不会去计算这个变量中的值。

%r{String}用于创建一个正则表达式字面值
就像使用/reg/方式一样,看代码:

1. result = %r{world}
2. puts result =~ "hello world"
3. puts "result is: #{result}, Type is:#{result.class}"

结果: 6
result is: (?-mix:world), Type is:Regexp

可以看出,world从第6个字符开始匹配

%w{String}用于将一个字符串以空白字符切分成一个字符串数组,进行较少替换
%W{String}用于将一个字符串以空白字符切分成一个字符串数组,进行较多替换
这两个应该是大家见过最多的,用这个方式构造数组,可以省下一些逗号,Ruby真 是会惯坏大家,以后大家都不用标点符号了。
同样给一个简单的例子:

1. result = %w{hello world}
2. puts "result is: #{result}, Type is:#{result.class}, length is:#{result.length}"

结果: result is: helloworld, Type is:Array, length is:2

%s{String}用于生成一个符号对象
直接先上代码:

1. result = %s{hello world}
2. puts "result is: #{result}, Type is:#{result.class}"
3. sym = :"hello world"
4. puts "the two symbol is the same: #{sym == result}"

结果:
result is: hello world, Type is:Symbol
the two symbol is the same: true

可以看出,这两中方式生成的symbol对象完全一样

%x{String}用于执行String所代表的命令
比如:
%x{notepad.exe}可以启动windows下的记事本,这里我就不列结果了(那是一个大家熟悉的窗口)


Ruby字符串处理函数总结列表

1.返回字符串的长度
str.length => integer

2.判断字符串中是否包含另一个串
str.include? other_str => true or false
"hello".include? "lo"
#=> true
"hello".include? "ol"
#=> false
"hello".include? ?h
#=> true

3.字符串插入:
str.insert(index, other_str)=> str
"abcd".insert(0, 'X')
#=> "Xabcd"
"abcd".insert(3, 'X')
#=> "abcXd"
"abcd".insert(4, 'X')
#=> "abcdX"
"abcd".insert(-3, 'X')
-3, 'X')
#=> "abXcd"
"abcd".insert(-1, 'X')
#=> "abcdX"

4.字符串分隔,默认分隔符为空格
str.split(pattern=$;, [limit]) => anArray
" now's the time".split
#=> ["now's", "the", "time"]
"1, 2.34,56, 7".split(%r{,\s*})
#=> ["1", "2.34", "56", "7"]
"hello".split(//)
#=> ["h", "e", "l", "l", "o"]
"hello".split(//, 3)
#=> ["h", "e", "llo"]
"hi mom".split(%r{\s*})
#=> ["h", "i", "m", "o", "m"]
"mellow yellow".split("ello")
#=> ["m", "w y", "w"]
"1,2,,3,4,,".split(',')
#=> ["1", "2", "", "3", "4"]
"1,2,,3,4,,".split(',', 4)
#=> ["1", "2", "", "3,4,,"]

5.字符串替换
str.gsub(pattern, replacement)=> new_str
str.gsub(pattern) {|match| block }
=> new_str
"hello".gsub(/[aeiou]/, '*')
#=> "h*ll*" #将元音替换成*号
"hello".gsub(/([aeiou])/, '<\1>')
#=> "hll" #将元音加上尖括号,\1表示保留原有字符???
"hello".gsub(/./) {|s| s[0].to_s + ' '}
#=> "104 101 108 108 111 "

字符串替换二:
str.replace(other_str)=> str
s = "hello"
#=> "hello"
s.replace "world"
#=> "world" Ruby

6.字符串删除:
str.delete([other_str]+) => new_str
"hello".delete "l","lo"
#=> "heo"
"hello".delete "lo"
#=> "he"
"hello".delete "aeiou", "^e"
#=> "hell"
"hello".delete "ej-m"
#=> "ho"

7.去掉前和后的空格
str.lstrip => new_str
" hello ".lstrip
#=> "hello "
"hello".lstrip
#=> " hello "
"hello".lstrip.rstrip
#=> "hello"

8.字符串匹配
str.match(pattern)=> matchdata or nil

9.字符串反转
str.reverse => new_str
"stressed".reverse
#=> "desserts"

10.去掉重复的字符
str.squeeze([other_str]*) => new_str
"yellow moon".squeeze
#=> "yelow mon" #默认去掉串中所有重复的字符
" now is the".squeeze(" ")
#=> " now is the" #去掉串中重复的空格
"putters shoot balls".squeeze("m-z")
#=> "puters shot balls" #去掉指定范围内的重复字符

11.转化成数字
str.to_i=> str
"12345".to_i
#=> 12345

chomp和chop的区别:
chomp:去掉字符串末尾的\n或\r
chop:去掉字符串末尾的最后一个字符,不管是\n\r还是普通字符
"hello".chomp
#=> "hello"
"hello\n".chomp
#=> "hello"
"hello\r\n".chomp
#=> "hello"
"hello\n\r".chomp
#=> "hello\n"
"hello\r".chomp
#=> "hello"
"hello".chomp("llo")
#=> "he"
"string\r\n".chop
#=> "string"
"string\n\r".chop
#=> "string\n"
"string\n".chop
#=> "string"
"string".chop
#=> "strin"

ruby 取子串:
aa = "happy"
aa[0,1] 为'h' ,取子串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值