显示标签为“index”的博文。显示所有博文
显示标签为“index”的博文。显示所有博文

2009年5月6日星期三

ruby的字符型

单行字符串的输出:


str = 'hello the world'
p str    #=> "hello the world"
puts str    #=> hello the world



多行字符串的输出:


str = <<EOS
    This is test.
    Ruby, the Object Oriented Script Language.
EOS

puts str
    #=>This is test.
          Ruby, the Object Oriented Script Language.



字符串的结合:


str = 'hello'
puts str + ' the world'    #=> hello the world



字符串的追加:


str = 'hello'
str << ' the world'
puts str    #=> hello the world



生成重复的字符串:


str = 'hello '
puts str*3    #=> hello hello hello



大小写字母的变换:


str = 'I Love Ruby'
puts str.upcase    #=> I LOVE RUBY
puts str.downcase    #=> i love ruby



字符串中的部分字符的取得:


str = "Apple Banana Orange"
puts str[0..4]     #=> Apple
puts str[6, 6]     #=> Banana



字符串中的部分字符的置换:


str = "Apple Banana Apple Orange"
str[0..4] = "Vine"     #=> str = "Vine Banana Apple Orane"
str[5, 6] = "Lemon"     #=> str = "Vine Lemon Apple Orange"
str.sub("Apple", "Pine")     #=> str = "Pine Banana Apple Orange"
str.gsub("Apple", "Pine")     #=> str = "Pine Banana Pine Orange"



字符串中变数的展开:


value = 123
puts "value is #{value}"     #=> value is 123



字符串中方法的展开:


def sub(str)
    "Hello, #{str}."
end

puts "Say #{sub('Tomoya')}"     #=> Say Hello, Tomoya.



削除字符串头和尾的空格:


str = ' Hello, Ruby! '
p s.strip     #=> "Hello, Ruby!"




削除字符串尾部的换行:


str = 'Hello, Ruby!\n'
p str.chomp    #=> "Hello, Ruby!"
p str.strip    #=> "Hello, Ruby!"




字符型向数值型的转换:


str = '99'
i = 1
puts i + str.to_i    #=> 100



数值型向字符型的转换:


str = '99'
i = 1
p i.to_s + str    #=> "199"



字符型(数值型)向浮点小数型的转换:


str = '99'
puts str.to_f    #=> 99.0



下一个字符串的取得:


p "9".succ    #>= "10"
p "a".succ    #>= "b"
p "AAA".succ    #>= "AAB"
p "A99".succ    #>= "B00"
p "A099".succ    #>= "A100"



检查字符串中是否有相同字符出现:


str = "Apple Banana Apple Orange"

# 从左则开始查找 puts str.index("Apple")    #=> 0
puts str.index("Banana")    #=> 6
puts str.index("Apple", 6)    #=> 13

# 从右则开始查找 puts str.rindex("Apple")    #=> 13
puts str.rindex("Apple", 6)    #>= 0



字符串的居中,居左和居右:


str = "Ruby"
p str.center(10)     #=> " Ruby "
p str.ljust(10)    #=> "Ruby "
p str.rjust(10)    #=> " Ruby"

2009年5月3日星期日

字符串的查找

查找匹配字符
用正则表达式查找匹配字符时,使用 =~ 运算符.


# 查找含有全角空格和半角空格.

p ([\s\t] =~ 'a c')    #=>  1
p ([\s\t] =~ 'abc')    #=>  nil



查找匹配字符最初出现在位置
查找匹配字符最初出现在位置时,使用 String#index


p 'xxxabcabcabcxxx'.index('abc')    #=>  3
p 'xxxabcabcabcxxx'.index('.')     #=>  nil
p 'xxx..............xxx'.index('.')     #=>  3



查找匹配字符最后出现在位置
查找匹配字符最后出现在位置时,使用 String#rindex


p 'xxxabcabcabcxxx'.rindex('abc')    #=>  9
p 'xxxabcabcabcxxx'.rindex('.')     #=>  nil
p 'xxx..............xxx'.rindex('.')     #=>  16



取得匹配字符外的字符串
取得到匹配字符之后的字符串,使用 Regexp#match


m = /abc/.match('xxxabcabcabcxxx')
p m.post_match    #=>  "abcabcxxx"
m = 'xxxabcabcabcxxx'.match(/abc/)
p m.post_match    #=>  "abcabcxxx"



查找字符串中匹配的所有字符
查找字符串中匹配的所有字符,使用 String#scan


# 查找字符串中的所有字母.
str = "abc所\ndef有\nghi字符\n"
str.scan(/[a-z]+/){ |s|
    p s
}

#=>  "abc"
#=>  "def"
#=>  "ghi"



取得含有匹配字符的一行
含有匹配字符的复数行.使用 String#grep (Enumerble#grep)


# 输出含有字母 a 和 g 的一行.

str = "abc所\ndef有\nghi字符\n"
str.grep(/[ag]/){ |line|
    p line
}

#=>  "abc所\n"
#=>  "ghi字符\n"


以下的两种写法同上相同

str.grep(re){ |line|
    # 处理内容
}



str.each{ |line|
    if re =~ line
        # 处理内容
    end
}