I used the ** downcase ** method in my Rails tutorial. I haven't had a chance to use it before, so I would like to deepen my understanding while solving the problem this time. I would also like to take a brief look at related methods.
--Related methods
--Methods used in the following problems - start_with? - end_with?
--Practice --Problem --Answer --Another solution
--Summary
--References
| Method | How to use | 
|---|---|
| downcase | Convert uppercase to lowercase | 
| upcase | Convert lowercase letters to uppercase | 
| swapcase | Convert uppercase to lowercase and lowercase to uppercase | 
| capitalize | Convert first lowercase letters to uppercase | 
| Destructive method(!) | At the end of the above method**( ! )When you giveobject**Make changes to | 
start_with?
A method to check if it starts with the specified string
end_with?
A method to check if it ends with the specified string
There are any two strings. Write a program that compares strings and checks if the other string is included at the end of the string.
I'm using downcase because I don't want to distinguish between lowercase and uppercase. It works fine with all uppercase letters using the upcase method.
def end_other(a, b)
  #Convert to lowercase and store in variable
  a_down = a.downcase 
  b_down = b.downcase
  #The character string of b is included in the character string of a?Or is the string a in the string b?
  if a_down.end_with?(b_down) || b_down.end_with?(a_down)
    p 'True'
  else
    p 'False'
  end
end
#Method call
end_other('Apple', 'Ple')
end_other('orange', 'Range')
end_other('banana', 'Nana')
By using slice, the part specified by the argument is cut out.
For slice, please refer to here.
def end_other(a, b)
  #Convert to lowercase and store in variable
  a_down = a.downcase
  b_down = b.downcase
  #Get the length of a string
  a_len = a_down.length
  b_len = b_down.length
  #Cut the character of the character string of a from the character string of b and equal to the character string of a.?Or, cut the character of the character string of b from the character string of a and equal to the character string of a.?
  if b_down.slice(-(a_len)..-1) == a_down || a_down.slice(-(b_len)..-1) == b_down
    p 'True'
  else
    p 'False'
  end
end
#Method call
end_other('Apple', 'Ple')
end_other('orange', 'Range')
end_other('banana', 'Nana')
--You can easily convert from lowercase to uppercase, from uppercase to lowercase, etc. by using the methods provided by Ruby.
-Ruby 3.0.0 Reference Manual (downcase)
-Ruby 3.0.0 Reference Manual (upcase)
-Ruby 3.0.0 Reference Manual (capitalize)
-Ruby 3.0.0 Reference Manual (swapcase)
-Ruby 3.0.0 Reference Manual (start_with?)
-Ruby 3.0.0 Reference Manual (end_with?)
-Summary of methods and usages that can be used with Ruby strings
-What is the slice method? Let's solve the example and understand the difference from slice!
-[Ruby] Extracting elements with slice method
Recommended Posts