Reverse ( ) Method : This method is used to reverse the entire string.
Example :
var1 = 'mukesh'
var2 = 'tomar'
var3 = '....hello mukesh'
puts var1.reverse
puts var2.reverse
puts var3.reverse
puts var1
puts var2
Output :
hsekum
ramot
hsekum olleh....
mukesh
tomar
As you can see in above method, it doesn't change the original string , it just reverse the entire string .That's var1 is still unchanged even after we called on it .
Another method is string length , using this method we get the number of characters(including spaces) in the string .
Example :
puts 'hello my name is'
name = gets
puts name.length.to_s // convert num to string
puts name
Output
hello my name is
mukesh tomar // using gets
12
mukesh tomar
Third method is to convert string in lowercase,uppercase,swapcase,capitalize .
Example
name = "mUkesh"
puts name.upcase
puts name.downcase
puts name.swapcase
puts name.capitalize
puts name
Output :
MUKESH
mukesh
MuKeSh
Mukesh
mukesh
0 Comment(s)