[Ruby] string processing

Source: Internet
Author: User
Tags chop

1. Return the length of the string
Str. Length => integer

2. Determine whether the string contains another string
Str. Include? Other_str => true or false

"Hello". Include? "Lo" # => true
"Hello". Include? "Ol" # => false
"Hello". Include? ? H #=> true

3. String insertion:
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') # => "abxcd"
"ABCD". insert (-1, 'x') # => "abcdx"

4. String separation. The default Delimiter is space.
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
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 | s [0]. to_s + ''}#=>" 104 101 108 108"

String replacement 2:
Str. Replace (other_str) => Str
S = "hello" # => "hello"
S. Replace "world" # => "world"

6. String deletion:

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
Str. lstrip => new_str

"Hello". lstrip # => "hello"
"Hello". lstrip # => "hello"

8. String Matching
Str. Match (pattern) => matchdata or nil

9. String Inversion
Str. Reverse => new_str

"Stressed". Reverse # => "desserts"

10. Remove duplicate characters
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
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.

"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"
"X". Chop. Chop # => ""

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.