1. Returns the length of the string
Copy Code code as follows:
2. Determine if the string contains another string
Copy Code code 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 Code code 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 delimited, the default separator is a space
Copy Code code as follows:
Str.split (pattern=$;, [limit]) => Anarray
"Now's the time". Split #=> ["Now", "the", "Time"]
"1, 2.34,56, 7". 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 Code code as follows:
Str.gsub (pattern, replacement) => New_str
Str.gsub (pattern) {|match| block} => new_str
"Hello". Gsub (/[aeiou]/, ' * ') #=> "h*ll*" #将元音替换成 *
"Hello". Gsub ([aeiou]/, ' <\1> ') #=> "h<e>ll<o>" #将元音加上尖括号, \1 to preserve the original characters???
"Hello". Gsub (/./) {|s| s[0].to_s + '} #=> ' 104 101 108 108 111 '
String Substitution Two:
Copy Code code as follows:
Str.replace (OTHER_STR) => str
s = "Hello" #=> "Hello"
S.replace "World" #=> "World"
6. String deletion
Copy Code code 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 before and after the space
Copy Code code as follows:
Str.lstrip => New_str
"Hello". Lstrip #=> "Hello"
"Hello". Lstrip #=> "Hello"
8. String matching
Copy Code code as follows:
Str.match (pattern) => Matchdata or nil
9. String inversion
Copy Code code as follows:
Str.reverse => New_str
"stressed". Reverse #=> "Desserts"
10. Remove duplicate characters
Copy Code code as follows:
Str.squeeze ([other_str]*) => New_str
"Yellow Moon". Squeeze #=> "Yelow mon" #默认去掉串中所有重复的字符
Squeeze ("") #=> "now is" #去掉串中重复的空格
"Putters shoot Balls". Squeeze (m-z) #=> "Puters shot Balls" #去掉指定范围内的重复字符
11. Convert into Digital
Copy Code code as follows:
Str.to_i=> Str
"12345". To_i #=> 12345
The difference between chomp and chop:
Chomp: remove \ n or \ r from the end of the string
Chop: Remove the last character at the end of the string, whether it is \n\r or plain character
Copy Code code 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"
Split is a class method for the String class, and I simply translate it based on what RI String.Split provides.
-----------------------------------------------------------String#split
Str.split (pattern=$;, [limit]) => Anarray
------------------------------------------------------------------------
Divides _str_ into substrings based on a delimiter, returning
Array of these substrings.
Splits a string into substrings, and returns an array that contains the substrings.
If _pattern_ is a +string+, then its contents are used as the
Delimiter when splitting _str_. If _pattern_ is a,
_str_ is split on whitespace, with leading whitespace and runs of
contiguous whitespace characters ignored.
If the pattern part is a string, it is delimited, and if pattern is a space, the space is split and the adjacent spaces are ignored.
If _pattern_ is a +regexp+, _str_ are divided where the pattern
Matches. Whenever the pattern matches a zero-length string, _str_
is split into individual characters.
If pattern is a regular expression, it is divided in the place where pattern is matched, and when pattern is a string of length 0, then split splits the string into a single character
If _pattern_ is omitted, the value of +$;+ is used. If +$;+ is
+nil+ (which is the default), _str_ are split on whitespace as if '
' were specified.
If pattern is ignored, it will be separated by $; if $; not set (that is, in default state), split will draw a space '
If the _limit_ parameter is omitted, trailing null fields are
Suppressed. If _limit_ is a positive number, at most that number of
Fields'll be returned (if _limit_ are +1+, the entire string is
Returned as the only entry in an array). If negative, there is no
Limit to the number of fields returned, and trailing null fields
are not suppressed.
If the limit parameter is ignored, the trace empty segment is suppressed, and if the limit is a positive number, then the Limit field is returned at most (if it is 1, then the entire string is returned as a field), and if it is a negative number, then the trace empty segment is not suppressed.
"Now's the time". Split #=> ["Now", "the", "Time"]
"Now's the Time". Split (") #=> [" Now "," the "," Time "]
"Now's the Time". Split (//) #=> ["", "Now", "" "," "," Time "]
"1, 2.34,56, 7". 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,,"]
"1,2,,3,4,,,". Split (', ',-4) #=> ["1", "2", "", "3", "4", "", ""]
If you include special characters, be aware of escaping
"Wo | Shi | Yi | GE | Bing ". Split (/\s*\|\s*) #竖杠别忘了转义
There is also the difference between it and String.scan, the pattern in Split is the separator, and the pattern in scan refers to what is to be matched.
"123=342=4234=523421=6424". Scan (/\d+/) #=> ["123", "342", "4234", "523421", "6424"]
If the match is enclosed, the delimiter is preserved, for example:
"Three Little Words". Split (/\s+/) #===>["Three", "little", words "]
"Three Little Words". Split (/(\s+)/) #===>["Three", "", "Little", "", "words"] reserved spaces