Some basic methods for operating strings IN Ruby: ruby operating strings

Source: Internet
Author: User
Tags printable characters unpack

Some basic methods for operating strings IN Ruby: ruby operating strings

One or more bytes of any sequence held and manipulated by the String object in Ruby, usually represented by characters in human language.

Simple string text is enclosed in single quotes (single quotes ). String Value of text in quotation marks:

'This is a simple Ruby string literal'

If you need to put a single quotes, single quotes string text, add a backslash before it. The Ruby interpreter does not consider the termination string:

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

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

The following are Ruby's string-related functions.
Expression replacement:

Expression replacement embedding values of any Ruby expressions into strings is a method used, which can be replaced by # {and:

#!/usr/bin/rubyx, y, z = 12, 36, 72puts "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 produces the following results:

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

String that is generally separated:

A string that is generally separated. You can create a string to match any separator. For example, although it is contained in a pair, such :.,!, (, {, <, And so on, with a percent sign (%). Q, q, and x added in front. strings with special meanings can be separated.

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

The following table uses escape characters or backslash characters to indicate the list of non-printable characters.

Note: Characters in double quotation marks are interpreted as escape characters and the escape characters in a single quotation mark character are retained.

Character encoding method:

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

You can change the character set using $ KCODE in the starting line of the program, as shown in the following code:

$KCODE = 'u'

Possible values of $ KCODE:

Built-in method of string:

We need a String object instance to call a String method. Use the following method to create an instance of a String object:

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

This will return a New String object containing a copy of str. The str object can be used in any available instance method. For example:

#!/usr/bin/rubymyStr = String.new("THIS IS TEST")foo = myStr.downcase
puts "#{foo}"

This produces the following results:

this is test

String decompression command:

The following table lists the methods for understanding the pressure command String # unpack.

Example:

Try the following example to extract 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"]

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.