String manipulation with Ruby on Rails
I made a little list of the methods that I use the most, to serve as a quick guide to string manipulation for RoR.
I hope it can help you too…
The first thing you should know about is that methods with names ending with “!” will modify the string receiver and methods without a “!” will just return a new string.
Knowing that, “method!” will actually modify the string and “method” will only modify the string for the moment it is being used.
Let’s first define a string.
Then some basic examples.
If you want to have the first letter of each word in your phrase Capitalized. ( uppercase the first letter of each word in the phrase )
Or also…
Or just replace a certain character. (replace “a” and “t” for “*”)
Or get rid os the spaces on the left and right, downcases the words and substitute the spaces in between the words for “_”. ( useful as uri friendly string )
The next will strip everything after the dot by making an array divided by the dot and using array reference to show just the desired pocket.
Reverse the phrase.
We can also apply simple math to strings
Reference:
David Felstead : click here
Ruby on Rails Examples : click here
Rails Brain : click here
1 comment posted
5 March, 2009
By: Pete K
Very nice use of the snipt embed, man :D I’m tweeting your post on @lionburger
His or Her website: click here...

