Ruby string-related methods

Source: Internet
Author: User

 

Construct string literal

Method 1:
A string enclosed by single or double quotation marks, such as "hello ".

Method 2:
% Q is used with the delimiter, and % Q represents single quotes.
STR = % Q! He/lo!

Method 3:
% Q is used with the delimiter, and % Q represents double quotation marks.
STR = % q {he/LO}

Method 4:
Here document builds strings. This method is suitable for creating strings with multiple rows. Start with <and boundary string and end with boundary string, for example, the following code:
STR = <end_of_string1
We are here now,
Where are you?
End_of_string1
Puts Str
Output result:
We are here now,
Where are you?

It is more complex to allow the appearance of multiple boundary string pairs.
STR = <end_of_string1, <end_of_string2
We are here now,
Where are you?
End_of_string1
I will leave now,
Wocould you like to go with me?
End_of_string2

Puts Str
Output result:
We are here now,
Where are you?
I will leave now,
Wocould you like to go with me?

 

Connection string

+ Connection. The + operator of Ruby will not automatically convert the right operand to a string. You must do it yourself:

Planet_number = 5
"Hello planet #" + planet_number.to_s # to_s converts to Str

In Ruby, using string interpolation is generally easier than using the + operator to connect. During string interpolation, the call to to_s is automatically performed:
"Hello planet # {planet_number }"

Literal and copy-on-write Technology

In Java, if the values of both string objects A and B are "abcdef", as follows:
String A = "abcdef ";
String B = "abcdef ";
Then, JVM will only create a constant object "abcdef", so that both A and B point to it. However, in ruby, an advanced technology copy-on-write is adopted, which is a smart pointer (familiar with C ++). It also shares the same character constant at the beginning, however, once an object (such as object B) is modified, "abcdef" will generate a copy, and B's modification will be performed on the copy.
For more details, see http://developer.51cto.com/art/200811/98630.htm.

Other differences with Java
Every time a Java string is modified, it does not change itself. Instead, it creates a new String object, and Ruby modifies itself every time it is modified.

 

Length

Puts "hello". Length

 

A string consisting of a single character.
S = "hello"
S [0] # "H"
S [S. Length-1]
Important:
If you try to access a character that exceeds the end of the string, Ruby will not throw an exception, but simply return nil.


Assignment:
The right side of the value assignment can be any string that can contain both multiple characters and empty characters.
IRB (main): 067: 0> S = "hello"
=> "Hello"
IRB (main): 068: 0> S [-1] = "ABC"
=> "ABC"
IRB (main): 069: 0> S
=> "Hellabc"
IRB (main): 070: 0>

 

S [-S. Length] is equal to s [1]

S [S. Length] is equal to nil

Obtain substrings:
Square brackets use two well-separated operands. The first one specifies the index (which can be negative) and the second one specifies the length value (which must be non-negative ).

S = "hello"
S [0, 2] # "he"
S [-1, 1] # "O": returns a string, not the character code? O
S [0, 0] # "": a zero-length substring is always empty
S [0, 10] # "hello": returns all the characters that are available
S [S. length, 1] # "": There is an empty string immediately beyond the end
S [S. Length + 1, 1] # nil: it is an error to read past that
S [0,-1] # nil: Negative lengths don't make any sense

 

Delete:
A non-null string is equivalent to a deleted string. If the left-side length is 0, it is equivalent to inserting:

S = "hello"
S [0, 1] = "H" # Replace first letter with a capital letter
S [S. length, 0] = "world" # append by assigning beyond the end of the string increase
S [5, 0] = "," # insert a comma, without deleting anything
S [5, 6] = "" # Delete with no insertion; S = "hellevels"

Another way to extract, insert, delete, or replace a substring is by indexing a string
With a range object. We'll explain ranges in detail in § 3. 5 later. For our purposes here,
A range is two integers separated by dots. When a range is used to index a string,
Return value is the substring whose characters fall within the range:

S = "hello"
S [2 .. 3] # "ll": characters 2 and 3
S [-3 ..-1] # "llo": Negative indexes work, too
S [0 .. 0] # "H": This range includes des one character index

S [0... 0] # "": This range is empty
S [2 .. 1] # "": This range is also empty
S [7 .. 10] # nil: This range is outside the string Bounds
S [-2 ..-1] = "P! "# Replacement: s becomes" Help! "
S [0... 0] = "please" # insertion: s becomes "Please help! "
S [6 .. 10] = "" # deletion: s becomes "please! "

 

Don't confuse string indexing with two comma-separated integers with this form that
Uses a single range object. Although both involve two integers, there is an important
Difference: the form with the comma specifies an index and a length; the form that uses
A range object specifies two indexes.

You can also use a string to index another string.

It is also possible to index a string with a string. When you do this, the return value is
The first substring of the target string that matches the index string, or nil, if no match
Is found. This form of string indexing is really only useful on the lefthand side of
Assignment statement when you want to replace the matched string with some other
String:
S = "hello" # start with the word "hello"
While (s ["L"]) # While the string contains the substring "L"
S ["L"] = "L"; # Replace first occurrence of "L" with "L"
End # Now we have "hello"
Finally, you can index a string using a regular expression. (Regular Expression objects
Are covered in § 9. 2.) The result is the first substring of the string that matches
Pattern, and again, this form of string indexing is most useful when used on
Lefthand side of an assignment:
S [/[aeiou]/] = '*' # Replace first vowel with an asterisk

 

 

Iterate the string

 

 

More:

Http://blog.csdn.net/csfreebird/article/details/4646140

 

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.