How to Remove the End of the String in Ruby
The string class in Ruby contains several functions for common string manipulations. For removing the end of a string, Ruby includes two functions that will perform the task, depending upon the degree of control you wish to have over the process.
Instructions
-
-
1
Enter the following line into the command prompt for Ruby to create a string:
a = "hello there. "
-
2
Type the following to automatically remove any white space (spaces, tabs, empty lines) from the end of the string:
a.strip!
-
-
3
Remove a specific set of characters from the end of the string with this line:
a.slice 0..-2
The command can be a little confusing. It tells Ruby to slice the string. This leaves only characters between 0, the first letter of the string, and -2, the next to last character of the string. This example would produce the string "hello there," with the last character "." removed.
-
1