Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • String Methods

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 179
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: