ruby中对字符串的操作

创建字符串

Ruby中创建一个字符串有多种方式。可以有两种方式表示一个字符串:用一对单引号包围字符('str')或用一对双引号包围字符("str") 这两种形式的区别在于对于包围的字符串的处理,用双引号构造的字符串能处理更多的转移字符。 

除了这两种方式,ruby还支持3种方式去构建字符串:%q%Qhere documents。 

%q后面用一对分界符包围的字符可以构造单引号字符串。 

%Q后面用一对分界符包围的字符可以构造双引号字符串。 

PS:分界符可以是任何一个非字母数字的单字节字符,如() [] {} <> //

here documents 

str=<<END_OF_STRING

  a string

END_OF_STRING

ruby中并不会去掉字符串开头的空格。 

 

#5种构建字符串hello world的方法对比

'hello world'

"hello world"

%q/hello world/

%Q{hello world}

str=<<EOS

  hello world

EOS

单引号和双引号在某些情况下有不同的作用.一个由双引号括起来的字符串允许字符由一个前置的斜杠引出,而且可以用#{}内嵌表达式.而 单引号括起来的字符串并不会对字符串作任何解释

 

Ruby的字符串操作比C更灵巧,更直观.比如说,你可以用+把几个串连起来,*把一个串重复好几遍

"foo" + "bar"   #"foobar" 

"foo" * 2      # #"foofoo"

抽取字符(注意:Ruby,字符被视为整数):

负的索引指从字符串尾算起的偏移量,

word[0]

word[-1]

herb[0,1] 

herb[-2,2] 

herb[0..3] 

herb[-5..-2] 

检查相等

"foo" == "foo" 

 

字符串基本操作

ruby中常用的简单字符串处理函数

split()

trim()

indexOf()

replaceAll()

String.split

"hello world".split( " ")

returns [ "hello", "world" ].

String.strip

" hello world ".strip

returns "hello world".

String.index

"hello world".index( "w")

returns 6.

String.gsub(/\s/, ',')

"hello word".gsub(\/s\, ',')

returns "hello,word"

p.s.

sub() replace first

gsub() replace all

 

1、字符串定义与产生

str1 = 'Hello world'

str2 = "Hello world"   #双引号比单引号定义的字符串更加强大,如可提供转移字符等

str3 = %q/Hello world/ # %q将后面的字符串转换成单引号字符串,后面的/为自定义的特殊符号,在字符串结尾处也需有该特殊符号

str4 = %Q/Hello world/ # %Q将定义双引号字符串

str = <<The_Text Hello World! Hello Ruby. The_Text

puts str        #这种方式比较有意思,str的内容为<<The_Text到下个The_Text之间的内容,The_Text为自定义的文本

arr = [1,1,1,2,2]

puts arr.join(",")    #数组用join转换成字符串

2、字符串操作

str = 'this' + " is"

str += ' you'

str <<" string"<<"."

puts str * 2 #this is you string.this is you string.

puts str[-12,12] # you string. 意味从后截取多少个字符

3、转义字符串

\n   \t  \'

字符串转移只对双引号字符串生效,例外为单引号,如:

str = 'this\'s you string.'

 

字符串内嵌入表达式用  #{ }

def Hello(name)

  "Hello #{neme}!"

end

 

4、删除

str.delete(str1,str2,...) 

#删除参数交集出现的所有字符,返回一个新字符串,如:

"hello world".delete("l") #返回"heo word"

"hello world".delete("lo","o") #返回"hell wrld"str.delete!(str1,str2,...) 

#直接对str进行删除操作,同时返回str如:

str="hello world"

str2=str.delete("l")  #str"hello world",str2"heo word"

str.delete!("l") #str"heo word"

5.字符串替换

str.gsub(pattern, replacement) => new_str  

str.gsub(pattern) {|match| block } => new_str  

"hello".gsub(/[aeiou]/, '*') #=> "h*ll*" #将元音替换成*号  

"hello".gsub(/([aeiou])/, '<\1>') #=> "h<e>ll<o>" #将元音加上尖括号,\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" 

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" 

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 

 

12、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" 

13、长度

#求字符串长度,返回int

str.size

str.length

 

14、特殊字符处理

str.chop 

#删除字符串str的最后一个字符,并返回新字符串

#若字符串以\r\n结尾,则两个字符都删去

#若字符串为空串,则返回空串

"string\r\n".chop  #返回"string"

"string\n\r".chop  #返回"string\n"

"string".chop      #返回"strin"

"s".chop.chop      #返回""str.chop! 

--------------------------------------------------------------------------------

str.chompendstr) 

#删除str的后缀endstr

#如果未指定endstr,则删除回车换行符(\r\n\r\n)

"hello\r\n".chomp  #返回"hello"

"hello".chomp("lo")#返回"hel"

"hello".chomp("l") #返回"hello"str.chomp! 

 

15、Ruby字符串处理函数包括返回字符串长度函数;

"hello".include? "lo"

 

16、Ruby生成随机数和随机字符串

rand(100000)

def newpass( len )

      chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a

       newpass = ""

       1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }

       return newpass

end

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') #=> "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,,"] 

也可以指定切分符:

"apples, pears, and peaches".split(", ")      # ["apples", "pears", "and peaches"]

"lions and tigers and bears".split(/ and /)   # ["lions", "tigers", "bears"]

splite方法的第二个参数用来限制切分的返回结果个数,具体效果规则如下:

1.如果省略这个参数,则切分结果中末尾为null的结果将被压缩掉

2.如果是正数,则结果按照指定的限制数量进行切分,尾部的null结果也将会保留做为结果

3.如果是负数,则切分结果数量无限制,并且保留尾部的null结果。

例如:

str = "alpha,beta,gamma,,"

list1 = str.split(",")     # ["alpha","beta","gamma"]

list2 = str.split(",",2)   # ["alpha", "beta,gamma,,"]

list3 = str.split(",",4)   # ["alpha", "beta", "gamma", ","]

list4 = str.split(",",8)   # ["alpha", "beta", "gamma", "", ""]

list5 = str.split(",",-1)  # ["alpha", "beta", "gamma", "", ""]

 

5、格式化字符串

======================================================================

Ruby的字符串格式话沿用了C的格式,在C中可用于sprintfprintf的格式话字符在Ruby中同样适用:

name = "Bob"

age = 28

str = sprintf("Hi, %s... I see you're %d years old.", name, age)

String类有个%方法,可以方面的做格式化的工作,它接受任何类型的单个值或一个数组:

str = "%-20s  %3d" % [name,age]

上面这个和下面这个式子是等效的

str = sprintf("%-20s  %3d", name, age)

我们还可以使用ljust,rjust,center方法来格式化我们的字符串:

str = "Moby-Dick"

s1 = str.ljust(13)    #"Moby-Dick    "

s2 = str.center(13)     #"  Moby-Dick  "

s3 = str.rjust(13)    #"    Moby-Dick"

 

6、控制字符串的大小写

==========================================

RubyString类提供了一组丰富的方法来控制大小写:

s = "Hello,World"

s1 = s.downcase    #"hello,world"

s2 = s.upcase     #"HELLO,WORLD"

capitalize方法把字符串第一个字符变大写,其余都是小写:

s3 = s.capitalize    #"Hello,world"

swapcase则是把字符串中的每个字符的大小写转换一下(原来大写的都小写,反之亦然)

s = "HELLO,world"

s1 = s.swapcase     #"hello,WORLD"

这些方法都有相应的in-place方法

 (upcase!,downcase!,capitalize!,swapcase!)

虽然,Ruby没有提供内置的判断字符是否是大小写的方法,但是,这不是问题,我们可以通过正则表达式来完成这一点:

if string =~ /[a-z]/

  puts "string contains lowercase charcters"

end

if string =~ /[A-Z]/

  puts "string contains uppercase charcters"

end

if string =~ /[A-Z]/ and string =~ /a-z/

  puts "string contains mixed case"

end

if string[0..0] =~ /[A-Z]/

  puts "string starts with a capital letter"

end

 

 


字符串的子串

=============================================

Ruby提供了多种访问操作字符串子串的方式,我们可以来看一下:

1.如果给出一组数字,则第一个数字代表取字符串的偏移位置,第二个数字表示

取的长度:

str = "Humpty Dumpty"

sub1 = str[7,4]         # "Dump"

sub2 = str[7,99]        # "Dumpty" (超过的长度按实际长度来取)

sub3 = str[10,-4]       # nil (长度为负数了)

记住,上面的形式,很多从别的语言转过来的ruby初学者会认为给出的两个数字是子串的开始和结束位置的偏移,这是错误的,务必记住。

给出的偏移是负数也是可以的,这样,位置将从字符串末尾开始计算:

str1 = "Alice"

sub1 = str1[-3,3]   # "ice"

str2 = "Through the Looking-Glass"

sub3 = str2[-13,4]  # "Look"

我们也可以给出一个Range来取子串:

str = "Winston Churchill"

sub1 = str[8..13]    # "Church"

sub2 = str[-4..-1]   # "hill"

sub3 = str[-1..-4]   # nil

sub4 = str[25..30]   # nil

强大的是,正则表达式在这个时候也充分发挥着作用:

str = "Alistair Cooke"

sub1 = str[/l..t/]   # "list"

sub2 = str[/s.*r/]   # "stair"

sub3 = str[/foo/]    # nil

如果给出的是一个字符串,则如果目标字符串中含有这个给出的字符串,则返回这个给出的字符串,否则返回nil

str = "theater"

sub1 = str["heat"]  # "heat"

sub2 = str["eat"]   # "eat"

sub3 = str["ate"]   # "ate"

sub4 = str["beat"]  # nil

sub5 = str["cheat"] # nil

如果给出的是一个数字,则返回的是该数字对应索引处字符的ASCII码:

str = "Aaron Burr"

ch1 = str[0]     # 65

ch1 = str[1]     # 97

ch3 = str[99]    # nil

同样,我们不仅可以通过上面的方式访问子串,还可以来向字符串设置内容:

str1 = "Humpty Dumpty"

str1[7,4] = "Moriar"     # "Humpty Moriarty"

str2 = "Alice"

str2[-3,3] = "exandra"   # "Alexandra"

str3 = "Through the Looking-Glass"

str3[-13,13]  = "Mirror" # "Through the Mirror"

str4 = "Winston Churchill"

str4[8..13] = "H"        # "Winston Hill"

str5 = "Alistair Cooke"

str5[/e$/] ="ie Monster" # "Alistair Cookie Monster"

str6 = "theater"

str6["er"] = "re"        # "theatre"

str7 = "Aaron Burr"

str7[0] = 66             # "Baron Burr"

重定义字符串的比较

=================================

字符串的比较<,<=,>,>=其实是四个方法,他们都会调用<=>这个方法,我们可以重新定义<=>来改变比较的行为:

class String

    alias old_compare <=>

    def <=>(other)

      a = self.dup

      b = other.dup

      a.gsub!(/[\,\.\?\!\:\;]/, "")

      b.gsub!(/[\,\.\?\!\:\;]/, "")

      a.gsub!(/^(a |an |the )/i, "")

      b.gsub!(/^(a |an |the )/i, "")

      a.strip!

      b.strip!

      a.old_compare(b)

  end

end

title1 = "Calling All Cars"

title2 = "The Call of the Wild"

未重定义之前,以下结果会是yes,但现在,将变成no

if title1 < title2

  puts "yes"

else

  puts "no"         

end

但是,==不会调用<=>,因此,如果我们要定义特殊的“比较是否相等”,则我们需要覆盖==这个方法。

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值