Examples of commonly used string processing functions in Ruby and ruby instances

Source: Internet
Author: User
Tags chop

Examples of commonly used string processing functions in Ruby and ruby instances

1. Return the length of the string
Copy codeThe Code is as follows:
Str. length => integer

2. Determine whether the string contains another string
Copy codeThe Code is as follows:
Str. include? Other_str => true or false
"Hello". include? "Lo" # => true
"Hello". include? "Ol" # => false
"Hello". include? ? H #=> true

3. String insertion
Copy codeThe Code is as follows:
Str. insert (index, other_str) => str
"Abcd". insert (0, 'x') # => "Xabcd"
"Abcd". insert (3, 'x') # => "abcXd"
"Abcd". insert (4, 'x') # => "abcdX"
"Abcd". insert (-3, 'x ')
-3, 'x') # => "abXcd"
"Abcd". insert (-1, 'x') # => "abcdX"

4. String separation. The default Delimiter is space.
Copy codeThe Code is as follows:
Str. split (pattern =$;, [limit]) => anArray
"Now's the time". split # => ["now's", "the", "time"]
& Quot; 1, 2.34, 56, 7 & quot ". split (% r {, \ s *}) # => ["1", "2.34", "56", "7"]
"Hello". split (//) # => ["h", "e", "l", "l", "o"]
"Hello". split (//, 3) # => ["h", "e", "llo"]
"Hi mom". split (% r {\ s *}) # => ["h", "I", "m", "o", "m"]
"Mellow yellow". split ("ello") # => ["m", "w y", "w"]
"1, 2, 3, 4,". split (',') # => ["1", "2", "", "3", "4"]
"1, 2, 3, 4,". split (',', 4) # => ["1", "2", "", "3, 4,"]

5. String replacement
Copy codeThe Code is as follows:
Str. gsub (pattern, replacement) => new_str
Str. gsub (pattern) {| match | block} => new_str
"Hello". gsub (/[aeiou]/, '*') # => "h * ll *" # Replace the vowel with the * sign
"Hello ". gsub (/([aeiou])/, '<\ 1>') # => "h <e> ll <o>" # enclose the vowel with Angle brackets, \ 1 indicates that the original characters are retained ???
"Hello". gsub (/./) {| s [0]. to_s + ''}#=>" 104 101 108 108"

String replacement 2:
Copy codeThe Code is as follows:
Str. replace (other_str) => str
S = "hello" # => "hello"
S. replace "world" # => "world"

6. delete a string
Copy codeThe Code is as follows:
Str. delete ([other_str] +) => new_str
"Hello". delete "l", "lo" # => "heo"
"Hello". delete "lo" # => "he"
"Hello". delete "aeiou", "^ e" # => "hell"
"Hello". delete "ej-m" # => "ho"

7. Remove leading and trailing Spaces
Copy codeThe Code is as follows:
Str. lstrip => new_str
"Hello". lstrip # => "hello"
"Hello". lstrip # => "hello"

8. String Matching
Copy codeThe Code is as follows:
Str. match (pattern) => matchdata or nil

9. String Inversion
Copy codeThe Code is as follows:
Str. reverse => new_str
"Stressed". reverse # => "desserts"

10. Remove duplicate characters
Copy codeThe Code is as follows:
Str. squeeze ([other_str] *) => new_str
"Yellow moon". squeeze # => "yelow mon" # All repeated characters in the string are removed by default.
"Now is the". squeeze ("") # => "now is the" # Remove repeated spaces in the string
"Putters shoot bils". squeeze ("m-z") # => "puters shot bils" # Remove repeated characters in the specified range

11. Convert to numbers
Copy codeThe Code is as follows:
Str. to_ I => str
"12345". to_ I # => 12345

Differences between chomp and chop:

Chomp: Remove \ n or \ r at the end of the string.
Chop: removes the last character at the end of the string, whether it is \ n \ r or a common character.

Copy codeThe Code is as follows:
"Hello". chomp # => "hello"
"Hello \ n". chomp # => "hello"
"Hello \ r \ n". chomp # => "hello"
"Hello \ n \ r". chomp # => "hello \ n"
"Hello \ r". chomp # => "hello"
"Hello". chomp ("llo") # => "he"

"String \ r \ n". chop # => "string"
"String \ n \ r". chop # => "string \ n"
"String \ n". chop # => "string"
"String". chop # => "strin"

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.