Some basic methods of manipulating strings in Ruby _ruby topics

Source: Internet
Author: User
Tags instance method unpack

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:

' 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:

' 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}:

#!/usr/bin/ruby

x, y, z = A,
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 results:

The value of x is.
The sum of x and Y is.
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.

%{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 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:

$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:

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:

#!/usr/bin/ruby

mystr = String.new ("This is TEST")
foo = mystr.downcase

Puts "#{foo}"

This will produce the following results:

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 \0\0abc \0\0". Unpack (' a6z6 ')  #=> ["abc", "ABC"]
"ABC \0\0". Unpack (' a3a3 ')      #=> ["abc", "\000\ ]
"ABC \0abc". Unpack (' z*z* ')    #=> ["abc", "ABC"]
"AA". Unpack (' B8b8 ')         #=> ["10000110", " 01100001 "]
" AAA ". Unpack (' h2h2c ')        #=> [", "", "]
" \xfe\xff\xfe\xff ". 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.