Ruby | String chop! Method
chop! is a String class method in Ruby which is used to return a new String with the last character removed. Both characters are removed if the string ends with \r\n, b. It will return nil if the string is empty.
Syntax:str.chop!
Parameters: Here, str is the given string.
Returns: A new string having no record seperator or nil if the string is empty.
Example 1:
# Ruby program to demonstrate # the chop! method # Taking a string and # using the method puts "".chop! puts "Ruby\r\n".chop! |
chevron_right
filter_none
Output:
Ruby
Example 2:
# Ruby program to demonstrate # the chop! method # Taking a string and # using the method puts "String\r\n\r\r\n".chop! # Removing two characters puts "Method".chop!.chop! |
chevron_right
filter_none
Output:
String Meth
Recommended Posts:
- Ruby | String chop Method
- Ruby | String == method
- Ruby | String =~ Method
- Ruby | String * Method
- Ruby | String << Method
- Ruby | String + Method
- Ruby | String hex Method
- Ruby | String === Method
- Ruby | String eql? Method
- Ruby | String <=> Method
- Ruby | String chr Method
- Ruby | String each_byte Method
- Ruby | String dump Method
- Ruby | String insert Method
- Ruby | String delete_suffix! Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


