Ruby 简明教程

Ruby - Strings

Ruby 中的 String 对象保存并处理一个或多个字节的任意序列,通常表示表示人类语言的字符。

A String object in Ruby holds and manipulates an arbitrary sequence of one or more bytes, typically representing characters that represent human language.

最简单的字符串文字用单引号(撇号字符)括起来。引号中的文本是该字符串的值——

The simplest string literals are enclosed in single quotes (the apostrophe character). The text within the quote marks is the value of the string −

'This is a simple Ruby string literal'

如果您需要将一个撇号放在一个单引号字符串文字中,请在前面加上一个反斜杠,这样 Ruby 解释器就不会认为它是终止字符串——

If you need to place an apostrophe within a single-quoted string literal, precede it with a backslash, so that the Ruby interpreter does not think that it terminates the string −

'Won\'t you read O\'Reilly\'s book?'

反斜杠还可以用来转义另一个反斜杠,这样第二个反斜杠就不会被解释为转义字符。

The backslash also works to escape another backslash, so that the second backslash is not itself interpreted as an escape character.

以下是 Ruby 的与字符串相关的特性。

Following are the string-related features of Ruby.

Expression Substitution

表达式替换是将任何 Ruby 表达式的值嵌入到一个字符串中的一种方式,使用 #{ 和 } ——

Expression substitution is a means of embedding the value of any Ruby expression into a string using #{ and } −

#!/usr/bin/ruby

x, y, z = 12, 36, 72
puts "The value of x is #{ x }."
puts "The sum of x and y is #{ x + y }."
puts "The average was #{ (x + y + z)/3 }."

这会产生以下结果 −

This will produce the following result −

The value of x is 12.
The sum of x and y is 48.
The average was 40.

General Delimited Strings

使用常规定界字符串,您可以在一对匹配的任意定界符(例如 !、(、{、< 等)内创建字符串,前面加上一个百分号字符 (%)。Q、q 和 x 具有特殊含义。常规定界字符串可以是——

With general delimited strings, you can create strings inside a pair of matching though arbitrary delimiter characters, e.g., !, (, {, <, etc., preceded by a percent character (%). Q, q, and x have special meanings. General delimited strings can be −

%{Ruby is fun.}  equivalent to "Ruby is fun."
%Q{ Ruby is fun. } equivalent to " Ruby is fun. "
%q[Ruby is fun.]  equivalent to a single-quoted string
%x!ls! equivalent to back tick command output `ls`

Escape Characters

Character Encoding

Ruby 的默认字符集是 ASCII,其字符可以用单个字节表示。如果您使用 UTF-8 或其他现代字符集,则字符可用一个至四个字节表示。

The default character set for Ruby is ASCII, whose characters may be represented by single bytes. If you use UTF-8, or another modern character set, characters may be represented in one to four bytes.

您可以在程序的开头使用 $KCODE 来更改字符集,如下所示——

You can change your character set using $KCODE at the beginning of your program, like this −

$KCODE = 'u'

String Built-in Methods

我们需要一个 String 对象的实例来调用 String 方法。以下是创建 String 对象实例的方法——

We need to have an instance of String object to call a String method. Following is the way to create an instance of String object −

new [String.new(str = "")]

这将返回一个包含 str 副本的新字符串对象。现在,使用 str 对象,我们可以使用任何可用的实例方法。例如——

This will return a new string object containing a copy of str. Now, using str object, we can all use any available instance methods. For example −

#!/usr/bin/ruby

myStr = String.new("THIS IS TEST")
foo = myStr.downcase

puts "#{foo}"

这会产生以下结果 −

This will produce the following result −

this is test

String unpack Directives

Example

尝试以下示例来解包各种数据。

Try the following example to unpack various data.

"abc \0\0abc \0\0".unpack('A6Z6')   #=> ["abc", "abc "]
"abc \0\0".unpack('a3a3')           #=> ["abc", " \000\000"]
"abc \0abc \0".unpack('Z*Z*')       #=> ["abc ", "abc "]
"aa".unpack('b8B8')                 #=> ["10000110", "01100001"]
"aaa".unpack('h2H2c')               #=> ["16", "61", 97]
"\xfe\xff\xfe\xff".unpack('sS')     #=> [-2, 65534]
"now = 20is".unpack('M*')           #=> ["now is"]
"whole".unpack('xax2aX2aX1aX2a')    #=> ["h", "e", "l", "l", "o"]