Use the String#strip method to remove all spaces
The code is as follows |
|
"Hello". Strip #=> "Hello" "Tgoodbyern". Strip #=> "Goodbye" |
In addition, Lstrip and Rstrip remove the spaces at the left and right ends of the string, respectively
The code is as follows |
|
"Hello". Lstrip #=> "Hello" "Hello". Lstrip #=> "Hello" "Hello". Rstrip #=> "Hello" "Hello". Rstrip #=> "Hello" |
The above method is to return the string copy, such as to change the original string object, using string#strip!, string#lstrip!, string#rstrip!. Returns nil if the object does not change
The code is as follows |
|
"Hello". rstrip! #=> Nil |
The code implementation inserts a space for each of the two characters of a string, and finally returns a result string. It is also best to provide an inverse procedure for this string conversion process together.
code is as follows |
|
str = ' ABCD ' Str =~ s/(? <=w{2}) (? =w{2}/""/ str = "AB CD" puts Str.split.join ("). to_s |