Ruby中有用但不常用的String方法总结
2014-05-28 17:56
405 查看
String中有用但是不常用到的方法:
1)、self[substr]
当自身当中包含substr的时候。则生成并返回一致的字符串
irb(main):075:0> substr = "bar"
=> "bar"
irb(main):076:0> result = "foobar"[substr]
=> "bar"
irb(main):077:0> p result
"bar"
=> "bar"
irb(main):078:0> substr.eql?(result)
=> true
2)、capitalize、 capitalize!
将字符串的首字符转换为大写,其余的转换为小写
irb(main):095:0> xx = "abcdef"
=> "abcdef"
irb(main):096:0> xx
=> "abcdef"
irb(main):097:0> xx.capitalize
=> "Abcdef"
irb(main):098:0> xx
=> "abcdef"
irb(main):099:0> xx.capitailze!
irb(main):102:0> xx
=> "Abcdef"
这里顺便说明一下方法尾部带!跟不带!的区别,其实通过上边的例子已经很清楚的看到了。带!的是直接对对字符串内容进行修改,而不带!的只是对字符串进行规定的操作,并不对字符串本身造成破坏。
3)、casecmp(other)
此方法跟 “<=>”方法类似,但是它忽略大小写
irb(main):105:0> "A".casecmp("a")
=> 0
irb(main):106:0> "A"<=>"a"
=> -1
4)、center、 ljust、 rjust、 center(length, str)、 ljust(length, str)、 rjust(length, str)
irb(main):107:0> "foo".center(20)
=> " foo "
irb(main):108:0> "foo".center(20,"*")
=> "********foo*********"
irb(main):109:0> "foo".ljust(20)
=> "foo "
irb(main):110:0> "foo".ljust(20,"*")
=> "foo*****************"
irb(main):111:0> "foo".rjust(20)
=> " foo"
irb(main):112:0> "foo".rjust(20,"*")
=> "*****************foo"
5)、count
该方法返回在该字符串中str所含字符出现的次数
irb(main):125:0> xx
=> "abc"
irb(main):126:0> "abcc".count("c")
=> 2
irb(main):127:0> "abcc".count("a-c")
=> 4
irb(main):128:0> "123345657688".count("23456")
=> 8
以上方法中如果“-”出现在中间就表示一个范围,当`^'出现在头部时表示"取反"
以下是该方法的一个应用场景
n_line = File.open("foo").read.count("\n") #=>统计文件的行数(文件尾部要有换行符)
6)、crypt
生成并返回一个由self和salt加密而成的字符串,salt一般是使用数字、字母、.以及下划线等构成的字符串,一般应选择尽量随机的字符串
salt = [rand(64),rand(64)].pack("C*").tr("\x00-\x3f","A-Za-z0-9./")
passwd.crypt(salt).
该方法一般很难利用加密后的字符串求出原字符串,所以一般用于用户密码加密
7)、delete(str1,str2, ...)、 delete!(str1,str2, ...)
从该字符串中删除str所包含的字符。若出现多个参数,则组多个参数的交集
irb(main):129:0> "123454656546789".delete("2-8","^4-6")
=> "14546565469"
irb(main):130:0> "234545667".delete("345")
=> "2667"
改方法会返回修改后的字符串,而delete!会修改原字符串。
8)、each_line、 each_byte
对字符串中的各行进行迭代,对字符串的各个字符进行迭代
“abcdefgh”.each_byte {|e| print e, " "} #=>97 98 99 100 101 102 103 104 => "abcdefgh"
8)、gsub(pattern, replace)、 gsub(pattern) {|matched| .... }
当带有replace参数的时候,原字符串就通过匹配pattern,然后将匹配的字符用replace替换。但是当不带replace参数的时候就可以当一个迭代器使用,这个时候pattern就会被当做一个参数被传递到改迭代器块。这个用法比较强大。
irb(main):138:0> "abcabc".gsub(/b/,"B")
=> "aBcaBc"
irb(main):139:0> "abcabc".gsub(/b/) {|e| e.upcase!}
=> "aBcaBc"
同样,带有!的会对字符串自身进行修改。
(这个方法还有待再去研究)
9)、intern、 to_sym
以上两个方法的使用方法基本类似,都是将一个字符串转化为一个符号
irb(main):140:0> "abc".intern
=> :abc
irb(main):141:0> "abc".to_sym
=> :abc
irb(main):142:0> "cat and dog".intern
=> :"cat and dog"
10)、next、 next!、 succ、 succ!
返回下一个字符串,是按照26个英文字母的顺序或者10进制数的顺序返回
irb(main):145:0> "abc".next
=> "abd"
irb(main):146:0> "fg11".succ
=> "fg12"
11)、oct(将字符串看成8进制字符串), hex(将字符串看成是10进制字符串)
12)、squeeze([str[,str2[, ... ]]])
压缩由str所含字符构成的重复字符串,`a-c'表示从a到c,而像"^0-9"这样,当`^'出现在头部时表示"取反",若给出多个参数,则意味着会使用所有参数的交集进行压缩
irb(main):147:0> "11112345565656".squeeze
=> "1234565656"
irb(main):148:0> "11112345565656".squeeze("1")
=> "12345565656"
13)、strip、 strip!
删除头部和尾部的所有空白字符串,空白字符是指" \t\r\n\f\v"
irb(main):149:0> "abc ".strip
=> "abc"
irb(main):150:0> "abc\t\n".strip
=> "abc"
13)、lstrip、lstrip!
删除字符串头部的所有空白字符。空白字符是指" \t\r\n\f\v"。
14)、rstrip 、rstrip!
删除字符串尾部的所有空白字符。空白字符是指" \t\r\n\f\v"。
15)、swapcase、swapcase!
将所有的大写换成小写,小写换成大写
1)、self[substr]
当自身当中包含substr的时候。则生成并返回一致的字符串
irb(main):075:0> substr = "bar"
=> "bar"
irb(main):076:0> result = "foobar"[substr]
=> "bar"
irb(main):077:0> p result
"bar"
=> "bar"
irb(main):078:0> substr.eql?(result)
=> true
2)、capitalize、 capitalize!
将字符串的首字符转换为大写,其余的转换为小写
irb(main):095:0> xx = "abcdef"
=> "abcdef"
irb(main):096:0> xx
=> "abcdef"
irb(main):097:0> xx.capitalize
=> "Abcdef"
irb(main):098:0> xx
=> "abcdef"
irb(main):099:0> xx.capitailze!
irb(main):102:0> xx
=> "Abcdef"
这里顺便说明一下方法尾部带!跟不带!的区别,其实通过上边的例子已经很清楚的看到了。带!的是直接对对字符串内容进行修改,而不带!的只是对字符串进行规定的操作,并不对字符串本身造成破坏。
3)、casecmp(other)
此方法跟 “<=>”方法类似,但是它忽略大小写
irb(main):105:0> "A".casecmp("a")
=> 0
irb(main):106:0> "A"<=>"a"
=> -1
4)、center、 ljust、 rjust、 center(length, str)、 ljust(length, str)、 rjust(length, str)
irb(main):107:0> "foo".center(20)
=> " foo "
irb(main):108:0> "foo".center(20,"*")
=> "********foo*********"
irb(main):109:0> "foo".ljust(20)
=> "foo "
irb(main):110:0> "foo".ljust(20,"*")
=> "foo*****************"
irb(main):111:0> "foo".rjust(20)
=> " foo"
irb(main):112:0> "foo".rjust(20,"*")
=> "*****************foo"
5)、count
该方法返回在该字符串中str所含字符出现的次数
irb(main):125:0> xx
=> "abc"
irb(main):126:0> "abcc".count("c")
=> 2
irb(main):127:0> "abcc".count("a-c")
=> 4
irb(main):128:0> "123345657688".count("23456")
=> 8
以上方法中如果“-”出现在中间就表示一个范围,当`^'出现在头部时表示"取反"
以下是该方法的一个应用场景
n_line = File.open("foo").read.count("\n") #=>统计文件的行数(文件尾部要有换行符)
6)、crypt
生成并返回一个由self和salt加密而成的字符串,salt一般是使用数字、字母、.以及下划线等构成的字符串,一般应选择尽量随机的字符串
salt = [rand(64),rand(64)].pack("C*").tr("\x00-\x3f","A-Za-z0-9./")
passwd.crypt(salt).
该方法一般很难利用加密后的字符串求出原字符串,所以一般用于用户密码加密
7)、delete(str1,str2, ...)、 delete!(str1,str2, ...)
从该字符串中删除str所包含的字符。若出现多个参数,则组多个参数的交集
irb(main):129:0> "123454656546789".delete("2-8","^4-6")
=> "14546565469"
irb(main):130:0> "234545667".delete("345")
=> "2667"
改方法会返回修改后的字符串,而delete!会修改原字符串。
8)、each_line、 each_byte
对字符串中的各行进行迭代,对字符串的各个字符进行迭代
“abcdefgh”.each_byte {|e| print e, " "} #=>97 98 99 100 101 102 103 104 => "abcdefgh"
8)、gsub(pattern, replace)、 gsub(pattern) {|matched| .... }
当带有replace参数的时候,原字符串就通过匹配pattern,然后将匹配的字符用replace替换。但是当不带replace参数的时候就可以当一个迭代器使用,这个时候pattern就会被当做一个参数被传递到改迭代器块。这个用法比较强大。
irb(main):138:0> "abcabc".gsub(/b/,"B")
=> "aBcaBc"
irb(main):139:0> "abcabc".gsub(/b/) {|e| e.upcase!}
=> "aBcaBc"
同样,带有!的会对字符串自身进行修改。
(这个方法还有待再去研究)
9)、intern、 to_sym
以上两个方法的使用方法基本类似,都是将一个字符串转化为一个符号
irb(main):140:0> "abc".intern
=> :abc
irb(main):141:0> "abc".to_sym
=> :abc
irb(main):142:0> "cat and dog".intern
=> :"cat and dog"
10)、next、 next!、 succ、 succ!
返回下一个字符串,是按照26个英文字母的顺序或者10进制数的顺序返回
irb(main):145:0> "abc".next
=> "abd"
irb(main):146:0> "fg11".succ
=> "fg12"
11)、oct(将字符串看成8进制字符串), hex(将字符串看成是10进制字符串)
12)、squeeze([str[,str2[, ... ]]])
压缩由str所含字符构成的重复字符串,`a-c'表示从a到c,而像"^0-9"这样,当`^'出现在头部时表示"取反",若给出多个参数,则意味着会使用所有参数的交集进行压缩
irb(main):147:0> "11112345565656".squeeze
=> "1234565656"
irb(main):148:0> "11112345565656".squeeze("1")
=> "12345565656"
13)、strip、 strip!
删除头部和尾部的所有空白字符串,空白字符是指" \t\r\n\f\v"
irb(main):149:0> "abc ".strip
=> "abc"
irb(main):150:0> "abc\t\n".strip
=> "abc"
13)、lstrip、lstrip!
删除字符串头部的所有空白字符。空白字符是指" \t\r\n\f\v"。
14)、rstrip 、rstrip!
删除字符串尾部的所有空白字符。空白字符是指" \t\r\n\f\v"。
15)、swapcase、swapcase!
将所有的大写换成小写,小写换成大写
相关文章推荐
- OC总结----类的方法.继承.NSString类的常用方法.NSMutableString 类的方法.数组类.字典类.block语法以及数组排序高级.属性.category (分类,类目).延展,等
- String和StringBuffer 常用方法总结
- 学习中 常用到的string内置对象方法的总结
- ruby中常用的方法总结
- GUAVA常用方法总结整理(list map string concurrent file)
- String、Path、File、Directroy 常用方法总结
- STD中string的常用方法总结
- String 及 StringBuffer常用方法总结
- iOS NSMutableAttributedString常用方法总结
- STD中string的常用方法总结
- String和StringBuffer 常用方法总结
- JavaScript中字符串String的常用方法总结
- String 常用方法最优算法实现总结 (一)
- String常用方法总结
- String 常用方法最优算法实现总结 (二)
- STD中string的常用方法总结[zz]
- string类型常用方法总结
- String常用方法小总结
- javascript中数组(Array)对象和字符串(String)对象的常用方法总结
- String与StringBuffer常用方法总结