I have a string
string = '1234432198766789'
I want it to look like this:
"1234 4321 9876 6789"
What is the best way to do it? I am able to use Rails’ String extensions.
I don’t want to modify the original string, so insert method couldn’t be used.
Why the double
reverse? So that when the string’s length is not a multiple of four, the odd-sized group will be on the left:If you know your string will always be a multiple of four, or if you don’t mind the odd-sized group being on the right, then leave out the two calls to
reverse.