美文网首页
第 14 章 字符串类

第 14 章 字符串类

作者: 南北东西总相随 | 来源:发表于2017-10-07 18:07 被阅读0次

    第 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"
    

    相关文章

      网友评论

          本文标题:第 14 章 字符串类

          本文链接:https://www.haomeiwen.com/subject/fvzjyxtx.html