第 14 章 字符串类

第 14 章 字符串类

14.1 字符串的创建

>> str1 = "Hello"
=> "Hello"
>> str2 = 'World'
=> "World"

""可以内嵌一个表达式,''内不会进行特殊字符的转义。

>> moji = "Hi"
=> "Hi"
>> str1 = "#{moji}"
=> "Hi"
>> str2 = '#{moji}'
=> "\#{moji}"

14.1.1 使用 %Q%q

使用 %Q 相当于用 "" 创建字符串,使用 %q 相当于用 '' 创建字符串。

>> desc = %Q{'Hello' "World"}
=> "'Hello' \"World\""
>> str = %q|'Hello world!'|
=> "'Hello world!'"

14.1.2 使用 Here Document

Here Document 是源自于 Unix 的 shell 的一种程序写法,使用 << 来创建字符串。创建包含换行的长字符串用这个方法是最简单的。

一般将 EOF 或 EOB 作为结束标识符,用 <<- 可以使缩进整齐。

>> str = <<-EOB
    Hello
    World
EOB
=> "    Hello\n    World\n"

14.1.4 使用 ``

通过用命令的形式,可以得到命令的标准输出并将其转换为字符串对象。

>> puts `ls -al`
total 24
drwxr-xr-x   4 yuhuihuang  staff   136 Oct  7 17:51 .
drwxr-xr-x  17 yuhuihuang  staff   578 Sep 27 21:44 ..
-rw-r--r--@  1 yuhuihuang  staff  6148 Oct  7 17:51 .DS_Store
-rw-r--r--@  1 yuhuihuang  staff    25 Oct  7 17:30 test.rb
=> nil

专栏:printf 方法 与 sprintf 方法

>> n = 123
=> 123
>> printf("%d\n", n)
123
=> nil
>> printf("%4d\n", n)
 123
=> nil
# 位数不足,补零处理。
>> printf("%04d\n", n)
0123
=> nil
# 输出的结果一定会包含 + 或 -
>> printf("%+d\n", n)
+123
=> nil
>> n = "Ruby"
=> "Ruby"
>> printf("Hello, %s!\n", n)
Hello, Ruby!
=> nil
# 按靠右对齐的方式输出 8 位字符串
>> printf("Hello, %8s!\n", n)
Hello,     Ruby!
=> nil
# 按靠左对齐的方式输出 8 位字符串
>> printf("Hello, %-8s!\n", n)
Hello, Ruby    !
=> nil
>> sprintf("%d", 123)
=> "123"
>> sprintf("%04d", 123)
=> "0123"
>> sprintf("%+d", 123)
=> "+123"
>> sprintf("Hello, %s!\n", "Ruby")
=> "Hello, Ruby!\n"
>> sprintf("Hello, %8s!\n", "Ruby")
=> "Hello,     Ruby!\n"
>> sprintf("Hello, %-8s!\n", "Ruby")
=> "Hello, Ruby    !\n"

14.2 获取字符串的长度

>> "just another ruby hacker,".length
=> 25
>> "just another ruby hacker,".size
=> 25

若是中文字符串,则返回字符数,如果想获取字节数,用 bytesize

p '面向对象编程语言'.length
p '面向对象编程语言'.bytesize 

结果:

8
24

判断字符串是否为空

>> "".empty?
=> true
>> "foo".empty?
=> false

14.3 字符串的索引

>> str = 'Hello, World!'
=> "Hello, World!"
>> str[0]
=> "H"
>> str[3]
=> "l"
>> str[2, 4]
=> "llo,"

14.4 字符串的连接

  • 将两个字符串合并为新的字符串
>> hello = 'Hello, '
=> "Hello, "
>> world = 'World!'
=> "World!"
>> str = hello + world
=> "Hello, World!"
>> hello
=> "Hello, "
  • 扩展原有的字符串

方法 <<

>> hello = 'Hello, '
=> "Hello, "
>> world = 'World!'
=> "World!"
>> hello << world
=> "Hello, World!"
>> hello
=> "Hello, World!"

方法 concat()

>> hello = 'Hello, '
=> "Hello, "
>> world = 'World'
=> "World"
>> hello.concat(world)
=> "Hello, World"
>> hello
=> "Hello, World"

14.5 字符串的比较

>> 'aaa' == 'baa'
=> false
>> 'aaa' == 'aaa'
=> true
>> 'aaa' != 'baa'
=> true
>> 'aaa' != 'aaa'
=> false

14.6 字符串的分割

>> str = "Hello:World"
=> "Hello:World"
>> ary = str.split(/:/)
=> ["Hello", "World"]

14.7 换行符的使用方法

破坏性\删除字符 删除最后一个字符 删除换行符
非破坏性的 chop chomp
破坏性的 chop! chomp!
>> str = "abcde"
=> "abcde"
>> newstr = str.chop
=> "abcd"
>> str = "abcde"
=> "abcde"
>> newstr = str.chomp
=> "abcde"
>> str = "abcd\n"
=> "abcd\n"
>> newstr = str.chomp
=> "abcd"
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,316评论 19 139
  • 9.19--9.23 第7章 正则表达式 正则表达式是一个拆分字符串并查询相关信息的过程。 推荐练习网站: js ...
    如201608阅读 1,116评论 0 4
  • 在负情绪的环境中呆久了,都不知道春天的气息了,感觉都忘记了如何微笑。 或许是氛围本省影响了我,抑或是我自己思考的方...
    daisy丹阅读 177评论 0 1
  • 别让冬天刺骨的冰,冻住 你的夏天,在你未沉睡之前 勤晒你的被子,把阳光攒进褥子里 趁他正照耀得完全 也别被秋天悲戚...
    夏爻XY阅读 368评论 3 9
  • 又是柳新。 陈菲菲咬着嘴唇,绞着手指。她有什么好呢?关越才见了一面就问我要号码。关越真是的,审美真差。白长了一副好...
    黑甲阅读 374评论 0 0

友情链接更多精彩内容