1. String creation
- Single quotes are included, escape characters and embedded expressions #{} (interpolation characters) are not supported
str = 'hello world!'
- double quotes contain
str = "hello world!"
- Use %, %Q, %q (followed by <>, {}, ||, [], etc.)
str = %|\there is test!| puts str #=> here is test! str = %Q|\there is test!| puts str #=> here is test! str = %q|\there is test!| puts str #=> \there is test!
%Q is equivalent to " " to create a string, %q is equivalent to ' ' to create a string
Usage scenario: you need to construct a very long string, and it contains a lot of single quotes and double quotes - HERE document representation method (especially suitable for representing large paragraphs of text with multiple lines)
str = <<HERE This is a here document! You can enter " multi-line content " in this block HERE
here HERE must be written at the beginning of the line, otherwise it will not be considered as a closing identifierstr = <<-HERE
This is a here document!
You can enter " multiline content" in this block
HERE<<- closing identifier, spaces and tabs before the closing identifier will be ignored; so that it does not need to be written at the beginning of the line
Support for escape characters and embedded strings
2. Basic operation
- [] string element reference
str = 'Element reference test' puts str[1] #=> l puts str[0, 7] #=> Element puts str[0..3] #=> Elem puts str[-4, 4] #=> test puts str[/n([a-z])/] #=> nt puts str[/n([a-z])/, 1] #=> t puts str['ref'] #=> ref puts str['hello'] #=> nil
The above shows 6 methods of str[int], str[int, int], str[range], str[regexp], str[regexp, int], str[string]
- []= string element assignment
str = 'Element reference test' str[0] = 'e' puts str #=> element reference test str[0, 7] = 'ABCD' puts str #=> ABCD reference test
- *, +, <<
str = ' test ' puts str*3 # => testtesttest puts str + ' ! ' # => test! puts str # => test puts str << ' ! ' # => test! puts str # => test! < < will change the original object
3. Common methods
str.length, str.size #=> returns the length of the string str.concat(obj) #=> Append string, similar to << str.capitalize #=> Capitalize the first letter of the string, and lowercase the rest str.delete(string) #=> Delete the string and return the deleted part str.each_byte{ |int| ...} #=> Iterate over each byte in the string str.each_line{ |line| ...} #=> Iterate over each line of the string str.strip #=> Remove invisible characters (spaces) from the end of the string
str.chop #=>Remove the last character in str
str.chomp #=>Remove the record separator from the end of the string str.downcase # => string converted to lowercase str.upcase #=> convert string to uppercase str.include?(obj) #=> Return true if string is included, otherwise return false str.index(string) #=> Returns the position where the string string appears for the first time in the string; the rindex method starts to retrieve from the back str.reverse #=> reverse the string str.split(pattern, <limit> ) #=> Split the string str into several substrings based on the delimiter pattern and return it as an array str.scan(pattern) #=> Search for all matching patterns in str
str.sub( pattern, replacement ) #=>Replace the first string matching pattern with replacement
str.gsub(pattern, replacement) #=> Replace All strings matching pattern are replacement
str.count(string, ...) #=> Count the number of occurrences of string