字符串
String 与 Symbol 这两个类提供了表示文字与处理文字的功能。这两个类之间很不同。
字符串表示
一个字符串一般用一组引号包装:
"这就是一个字符串"
单引号也行:
'我也是个字符串'
使用字符插值的时候得用双引号:
puts "二加二等于 #{2 + 2}"
puts '二加二等于 #{2 + 2}'
输出的内容是:
二加二等于 4
二加二等于 #{2 + 2}
单引与双引在 escape 时的区别:
puts "Backslashes (\) have to be escaped in double quotes."
puts 'You can just type once in a single quoted string.'
puts "But whichever type of quotation mark you use..."
puts "...you have to escape its quotation symbol, such as "."
puts 'That applies to ' in single-quoted strings too.'
puts 'Backslash-n just looks like n between single quotes.'
puts "But it means newlinenin a double-quoted string."
puts 'Same with t, which comes out as t with single quotes...'
puts "...but inserts a tab character:tinside double quotes."
puts "You can escape the backslash to get \n and \t with double quotes."
输出的是:
Backslashes () have to be escaped in double quotes.
You can just type once in a single quoted string.
But whichever type of quotation mark you use...
...you have to escape its quotation symbol, such as ".
That applies to ' in single-quoted strings too.
Backslash-n just looks like n between single quotes.
But it means newline
in a double-quoted string.
Same with t, which comes out as t with single quotes...
...but inserts a tab character: inside double quotes.
You can escape the backslash to get n and t with double quotes.
下午6:59 **
下午7:24 **
其它的引用机制
还有一种引用机制,%char{text},下面的 %q 生成一个单引号字符:
puts %q{You needn't escape apostrophes when using %q.}
因为上面的字符串不是用单引号标记的,所以字符串里的撇号不用 escape 。
双引号用 %Q,%{} 也可以。分隔符也可以很随便:
%q-A string-
%Q/Another string/
%[Yet another string]
Here 文档
here-doc,可以是多行字符串:
>> text =
This is the first line of text.
This is the second line.
Now we're done.
EOM
=> "This is the first line of text.nThis is the second line.nNow we're done.n"
>> text =
The EOM doesn't have to be flush left!
EOM
=> "The EOM doesn't have to be flush left!n"
默认 here-docs 跟双引号字符串一样,可以解释它里面的字符插值,还有一些 escape 字符,比如 n 或 t 。如果你需要让 here-doc 像单引号字符串那样,可以这样:
>> text =
Single-quoted!
Note the literal n.
And the literal #{2+2}.
EOM
=> "Single-quoted!nNote the literal \n.nAnd the literal #{2+2}.n"
>> puts text
Single-quoted!
Note the literal n.
And the literal #{2+2}.
=> nil
这样再试试:
>> a =
5
EOM
=> 50
再这样试一下:
>> array = [1,2,3,
This is the here-doc.
It becomes array[3]
EOM
=> [1, 2, 3, "This is the here-doc.nIt becomes array[3]n", 4]
作为方法参数:
do_something_with_args(a, b,
http://*so*me_ver*y_long_url_or_other_text_best_put_on_its_own_line
EOM
字符串处理
获取与设置子字符串
[] 操作符可以得到一个字符串里的某个位置上的字符。提供一个索引号就行了,索引号是从零开始的,负数的索引指的是从字符串的最后往前数。
试一下:
>> string = 'Ruby is a cool language.'
=> "Ruby is a cool language."
>> string[5]
=> "i"
>> string[-12]
=> "o"
截取一段,再给 [] 提供一个 m 参数:
>> string[5,10]
=> "is a cool "
也可以用一个 range 对象作为参数:
>> string[7..14]
=> " a cool "
>> string[-12..-3]
=> "ol languag"
>> string[-12..20]
=> "ol langua"
>> string[15..-1]
=> "language."
也可以直接给出你想得到的子字符串的内容,如果找到了就返回这个字符串,没找到就返回 nil:
>> string['cool lang']
=> "cool lang"
>> string['very cool lang']
=> nil
正则表达式:
>> string[/c[ol ]+/]
=> "cool l"
slice 与 slice!:
>> string.slice!('cool')
=> "cool"
>> string
=> "Ruby is a language."
[]= 方法可以设置找到的字符串:
>> string = 'Ruby is a cool language.'
=> "Ruby is a cool language."
>> string['cool'] = 'great'
=> "great"
>> string
=> "Ruby is a great language."
>> string[-1] = '!'
=> "!"
>> string
=> "Ruby is a great language!"
>> string[-9..-1] = 'thing to learn'
=> "thing to learn"
>> string
=> "Ruby is a great thing to learn"
合并字符串
>> 'a' + 'b'
=> "ab"
>> 'a' + 'b' + 'c'
=> "abc"
+ 得到的永远都会是一个新的字符串,这样再试一下:
>> str = 'hi '
=> "hi "
>> str + 'there.'
=> "hi there."
>> str
=> "hi "
再试试
>> str = 'hi '
=> "hi "
>> str
=> "hi there."
>> str
=> "hi there."
通过插值合并字符串
>> str = 'hi'
=> "hi"
>> "#{str} there."
=> "hi there."
插值里可以是任何 Ruby 表达式:
>> "the sum is #{2+2}"
=> "the sum is 4"
不信再试试这个:
>> "My name is #{
>> class Person
>> attr_accessor :name
>> end
>> d = Person.new
>> d.name = 'David'
>> d.name
>> }."
=> "My name is David."
更好的方法:Ruby 在插值里会调用对象的 to_s 方法,你可以定义自己的 to_s 方法:
>> class Person
>> attr_accessor :name
>> def to_s
>> name
>> end
>> end
=> :to_s
>> david = Person.new
=> #<0x007fbde8a195c0>
0x007fbde8a195c0>