ruby data type String

1. String creation

  1. Single quotes are included, escape characters and embedded expressions #{} (interpolation characters) are not supported
    str  = 'hello world!'

     

  2. double quotes contain
    str = "hello world!"

     

  3. 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

  4. 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 identifier
    str = <<-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

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324646248&siteId=291194637