当前位置: 代码迷 >> 综合 >> ruby 字符串处理
  详细解决方案

ruby 字符串处理

热度:58   发布时间:2024-01-16 05:09:00.0
1.建立字符串
1>  
title = String.new #new方法创建字符串
str = String.new("hello world")
str = String("hello world")
str = "hello world"
title.empty? #新建的字符串是否为空字符串
title.length || title.size #检查字符串是否为空
2>双引号会转移字符,单引号则予以保留

2.通用分隔字符串,以%为字首并以一组分界符(!!、{}、[])括起字符串内容
str_com = %!Hello world!
str_tory = %{Hello sean}
str_tray = %[Hello all]

%Q=双引号括起,  %q=单引号括起  %x=`用在指令的输出

3.嵌入文档
str = <<30
aaaaaaaa
bbbbbbbb
cccccccc
30

str.freeze #冻结字符串
str.frozen?

4.访问字符串
str = "hello world"
str["hello"] #return true
str[0].chr # return "h"
str[1,5]
str[1,5],capitalize #大写形式输出
str[1..5] #包括最后一个字符
str[1...5]
str.index("1")
str[/hell$/]
str[/^hell/,0]


5.