Ruby way 第二章【字符串】2 -- 关于字符串的内容可真多,有些东西一时半会用不到,可临时查

在字符串中做替换操作
=========================================
我们已经知道sub和gsub的基本用法了,我们来看它们的更加高级的用法。同时还有这两个方法

的in-place方法sub!和gsub!

s1 = "spam, spam, and eggs"
s2 = s1.sub(/spam/,"bacon")    # "bacon, spam, and eggs"

s3 = s2.sub(/(\w+), (\w+),/,'\2, \1,')  # "spam, bacon, and eggs"

s4 = "Don't forget the spam."
s5 = s4.sub(/spam/) { |m| m.reverse }   # "Don't forget the maps."

s4.sub!(/spam/) {  |m| m.reverse }      # s4 is now "Don't forget the maps."

如上面的例子中所示,\1,\2等符号可以用来把匹配到的结果作为替换的内容来使用

全局变量$&中存放着最后一次匹配到的内容。


搜索字符串
===========================================
index方法用来查找给出的子串,字符或正则表达式在字符串中的起始位置。如果没找到,则返

回nil
str = "Albert Einstein"

pos1 = str.index(?E)     # 7
pos2 = str.index("bert") # 2
pos3 = str.index(/in/)   # 8
pos4 = str.index(?W)     # nil
pos5 = str.index("bart") # nil
pos6 = str.index(/wein/) # nil

rindex方法将会从字符串的最右边开始查找:
str = "Albert Einstein"
pos1 = str.rindex(?E)     # 7
pos2 = str.rindex("bert") # 2
pos3 = str.rindex(/in/)   # 13 (finds rightmost match)
pos4 = str.rindex(?W)     # nil
pos5 = str.rindex("bart") # nil
pos6 = str.rindex(/wein/) # nil

include?方法用来判断字符串中是否包含所给出的内容:
str1 = "mathematics"
flag1 = str1.include? ?e         # true
flag2 = str1.include? "math"     # true
str2 = "Daylight Saving Time"
flag3 = str2.include? ?s         # false
flag4 = str2.include? "Savings"  # false

scan方法则会扫描整个字符串,把匹配结果收集起来:
str1 = "abracadabra"
sub1 = str1.scan(/a./)    #["ab","ac","ad","ab"]

str2 = "Acapulco, Mexico"
sub2 = str2.scan(/(.)(c.)/)    #[["A","ca"],["l","co"],["i","co"]]

如果使用了block,则匹配结果会被传到block中:
str3 = "Kobayashi"
str3.scan(/[^aeiou]+[aeiou]/) do |x|
    print "Syllable: #{ x}\n"
end

结果是:
Syllable: Ko
Syllable: ba
Syllable: ya
Syllable: shi


在字符和ASCII之间相互转换
============================================
在ruby中,字符本身就是整数:
str = "Martin"
print str[0]   #77

如果一个整数直接被追加到字符串中,则它会被转换成字符:
str2 = str << 111  #"Martino"

length和size方法的作用是一样的,都是用来计算字符串的长度:
str1 = "Carl"
x = str1.length      # 4
str2 = "Doyle"
x = str2.size      # 5


处理一行的字符串
======================================
Ruby一个字符串中可以包含很多行。举个例子,我们可以把一个很小的文件(其中有多行数据)读

取到内存中,存放在一个字符串中。字符串默认的each方法可以处理这些行。
str = "Once upon\na time...\nThe End\n"
num = 0
str.each do |line|
    num += 1
    print "Line #{ num} : #{ line}"
end
输出结果为:
Line 1 : Once upon
Line 2 : a time...
Line 3 : The End


处理字节
=========================================
字符串的each_byte迭代方法用来逐个处理字符串的每个字节
str = "ABC"
str.each_byte do |char|
    print char, " "
end
结果:
65 66 67


向字符串追加内容"<<操作符"
============================================
str = "A"
str << [1,2,3].to_s << " " << (3.14).to_s  # 结果:A123 3.14

如果0~255范围的数字追加到字符串中,则它会被转换成字符:
str = "Marlow"
str << 101 << ", Christopher"  # 结果: Marlowe, Christopher


删除尾部的换行符和其他字符
==============================================
使用chop方法来删除尾部字符:
str = gets.chop         # Read string, remove newline
s2 = "Some string\n"    # "Some string" (no newline)
s3 = s2.chop!           # s2 is now "Some string" also
s4 = "Other string\r\n"
s4.chop!                # "Other string" (again no newline)

记得,chop和chop!在字符串尾部没有换行符号的情况下,会删除最后一个非换行字符:
str = "ABC"
str.chop      # AB

如果不想要上面的效果,请用chomp:
str = "ABC"
str.chomp    # ABC

str = "xyz\n"
str.chomp    # xyz

chomp方法可以带一个参数,这样的作用是可以用来删除字符串尾部和参数匹配的内容:
str = "abcxyz"
str.chomp("xyz")   # abc

str1 = "abcxyz"
str1.chomp("D")    # abcxyz

 

 

你可能感兴趣的:(正则表达式,Ruby)