Some basic methods of manipulating strings in Ruby

Source: Internet
Author: User
Tags instance method unpack

This article mainly introduces some of the basic methods of manipulating strings in Ruby, including the processing of string compression and decompression, the need for friends can refer to the following

A string object in Ruby holds and manipulates one or more bytes of any sequence, usually representing the character representation of the human language.

Simple string literals are enclosed in single quotes (single quote characters). String value of the text within quotation marks:

?

1 ' This is a simple Ruby string literal '

If you need to place a single quote within a single quote string literal, precede it with a backslash ruby interpreter that does not consider the terminating string:

?

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

A backslash can also escape another backslash, so that the second backslash itself is not interpreted as an escape character.

The following are string-related features Ruby's.

Expression substitution:

An expression replaces the value of embedding any Ruby expression with a means of using a string, which can be replaced with #{and}:

?

1 2 3 4 5 6 #!/usr/bin/ruby x, y, z = A, puts "the value of x is #{x}." puts "the sum of x and Y are #{x + y}." Puts "the Average was #{(x + y + z)/3}. "

This will produce the following results:

?

1 2 3 The value of X is 12. The sum of x and Y is 48. The average was 40.

A generally delimited string:

A generally delimited string that can be created to match arbitrary delimiters, for example, although included in a pair, such as:.,!, ({,,, etc., preceded by a percent semicolon (%). Q, Q and X, have special meanings that can generally be delimited by strings.

?

1 2 3 4 %{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-quo Ted String%x!ls! Equivalent to back tick command output ' ls '

Escape character:

The following table is an escape or backslash symbol that can represent a list of nonprinting characters.

Note: In double quotes the string is interpreted as an escape character, and the escape character in a single quote character is retained.

Character encoding method:

Ruby's default character set is the ASCII code, which may be represented by a single-byte character. If you use UTF-8 or another modern character set, the character may be represented in four bytes.

You can change the character set using $kcode at the beginning of the program, like this:

?

1 $KCODE = ' U '

The following are $KCODE possible values

Built-in methods for strings:

We need to have a String object instance calling a string method. The following methods are used to create an instance of a string object:

?

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

This returns a new string object that contains a copy of Str. Now using the Str object, we can have any instance method available. For example:

?

1 2 3 4 #!/usr/bin/ruby mystr = String.new ("This is TEST") foo = mystr.downcase

?

1 Puts "#{foo}"

This will produce the following results:

?

1 This is test

String Decompression Instructions:

The following table lists the methods used to string#unpack the pressure instructions.

Example:

Try the following examples to decompress various data.

?

ABC. Unpack (' a6z6 ') #=> ["ABC", "ABC"] "abc". Unpack (' a3a3 ') #=> ["abc", "0000"] "abc abc". Unpack (' z*z* ') #=> ["ABC", "abc"] "AA". Unpack (' b8b8 ') #=> ["10000110", "01100001"] "AAA". Unpack (' h2h2c ') #=> ["16", "61", 97] "Xfexffxfexff". Unpack (' SS ') #=> [-2, 65534] "Now=20is". Unpack (' m* ') #=> [' Now '] ' whole '. Unpack (' XAX2AX2AX1AX2A ') #=> ["H", "E", "L", "L", "O"]
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.