字符串

Strings

字符串

Ruby deals with strings as well as numerical data. A string may be double-quoted (“…”) or single-quoted (‘…’).

Ruby处理字符串就同处理数值数据一样优秀。定义字符串可以用双引号或者单引号

  1. ruby> "abc"
  2. "abc"
  3. ruby> 'abc'
  4. "abc"

Double- and single-quoting have different effects in some cases. A double-quoted string allows character escapes by a leading backslash, and the evaluation of embedded expressions using #{}. A single-quoted string does not do this interpreting; what you see is what you get. Examples:

在某些场景中,双引号单引号会产生不同的结果。双引号字符串允许通过反斜线对字符进行转义,以及对使用#{}嵌入的表达式进行求值。单引号字符串则不对这些进行解释,你所看到的便是其最终的结果。比如:

  1. ruby> puts "a\nb\nc"
  2. a
  3. b
  4. c
  5. nil
  1. ruby> puts 'a\nb\n'
  2. a\nb\nc
  3. nil
  1. ruby> "\n"
  2. "\n"
  1. ruby> '\n'
  2. "\\n"
  1. ruby> "\001"
  2. "\t"
  1. ruby> '\001'
  2. "\001"
  1. ruby> "abcd #{5*3} efg"
  2. "abcd 15 efg"
  1. ruby> var = " abc "
  2. " abc "
  1. ruby> "1234#{var}5678"
  2. "1234 abc 5678"

Ruby’s string handling is smarter and more intuitive than C’s. For instance, you can concatenate strings with +, and repeat a string many times with *:

Ruby的字符串处理比C语言更智能,更直观。举个例子,你可以使用+把字符串连接起来,或者使用*将字符串重复多次。

  1. ruby> "foo" + "bar"
  2. "foobar"
  1. ruby> "foo" * 2
  2. "foofoo"

Concatenating strings is much more awkward in C because of the need for explicit memory management:

C语言中连接字符串要显得更笨拙,因为它需要显式地进行内存管理:

  1. char *s = malloc(strlen(s1)+strlen(s2)+1);
  2. strcpy(s, s1);
  3. strcat(s, s2);
  4. /* ... */
  5. free(s);

But using ruby, we do not have to consider the space occupied by a string. We are free from all memory management.

但是如果使用的是Ruby,我们则不需要考虑字符串占用的空间,由此从所有的内存管理工作中解放出来。

Here are some things you can do with strings.

这里有一些你可以用字符串完成的事情。

Concatenation:

连接:

  1. ruby> word = "fo" + "o"
  2. "foo"

Repetition:

重复:

  1. ruby> word = word * 2
  2. "foofoo"

Extracting characters (note that characters are integers in ruby):

取出字符(注意在Ruby中字符是整数):

  1. ruby> word[0]
  2. 102 # 102是`f'的ASCII码
  3. ruby> word[-1]
  4. 111 # 111是`o'的ASCII码

(Negative indices mean offsets from the end of a string, rather than the beginning.)

(负数的索引则表示从字符串末尾开始计算的偏移量,而不是从头开始计算。)

Extracting substrings:

取出子字符串:

  1. ruby> herb = "parsley"
  2. "parsley"
  3. ruby> herb[0,1]
  4. "p"
  5. ruby> herb[-2,2]
  6. "ey"
  7. ruby> herb[0..3]
  8. "pars"
  9. ruby> herb[-5..-2]
  10. "rsle"
  11. Testing for equality:
  12. ruby> "foo" == "foo"
  13. true
  14. ruby> "foo" == "bar"
  15. false

Now, let’s put some of these features to use. This puzzle is “guess the word,” but perhaps the word “puzzle” is too dignified for what is to follow ;-)

现在,让我们来使用一些这些特性。这个谜题是“猜单词”,但也许“谜”这个词太过凝重,不适合接下来的内容;-)

save this as guess.rb

保存为guess.rb

  1. words = ['foobar', 'baz', 'quux']
  2. secret = words[rand(3)]
  3. print "guess? "
  4. while guess = STDIN.gets
  5. guess.chop!
  6. if guess == secret
  7. puts "You win!"
  8. break
  9. else
  10. puts "Sorry, you lose."
  11. end
  12. print "guess? "
  13. end
  14. puts "The word was ", secret, "."

For now, don’t worry too much about the details of this code. Here is what a run of the puzzle program looks like.

现在,不要太担心这段代码的细节。下面是这个谜题程序的运行情况。

  1. ruby guess.rb
  2. guess? foobar
  3. Sorry, you lose.
  4. guess? quux
  5. Sorry, you lose.
  6. guess? ^D
  7. The word was baz.

(I should have done a bit better, considering the 1/3 probability of success.)

(考虑到1/3的成功概率,我应该可以做得更好一些。)

上一章 简单示例
下一章 正则表达式