Ruby Strings

by

in

Ruby string object holds and manipulates an arbitary sequence of bytes, typically representing characters. They are created using String::new or as literals.


Quotes

Ruby string literals are enclosed within single and double quotes.

Example:

#!/usr/bin/ruby   

  

puts 'Hello everyone'   

puts "Hello everyone"

Output:

Ruby string 1

Accessing string elements

You can access Ruby string elements in different parts with the help of square brackets []. Within square brackets write the index or string.

Example:

#!/usr/bin/ruby   

  

msg = "This tutorial is from JavaTpoint."   

  

puts msg["JavaTpoint"]   

puts msg["tutorial"]   

  

puts msg[0]   

  

puts msg[0, 2]   

puts msg[0..19]   

puts msg[0, msg.length]   

puts msg[-3]

Output:

Ruby string 2

Multiline string

Writing multiline string is very simple in Ruby language. We will show three ways to print multi line string.

  • String can be written within double quotes.
  • The % character is used and string is enclosed within / character.
  • In heredoc syntax, we use << and string is enclosed within word STRING.

Example:

    
    
    1. puts "   
    2. A   
    3. AB   
    4. ABC   
    5. ABCD"   
    6.   
    7. puts %/   
    8. A   
    9. AB   
    10. ABC   
    11. ABCD/   
    12.   
    13. puts <<STRING   
    14. A   
    15. AB   
    16. ABC   
    17. ABCD   
    18. STRING 

    Output:

    Ruby string 3

    Variable Interpolation

    Ruby variable interpolation is replacing variables with values inside string literals. The variable name is put between #{ and } characters inside string literal.

    Example:

    #!/usr/bin/ruby   
    
      
    
    country = "India"   
    
    capital = "New Delhi"   
    
      
    
    puts "#{capital} is the capital of #{country}."

    Output:

    Ruby string 4

    Concatenating Strings

    Ruby concatenating string implies creating one string from multiple strings. You can join more than one string to form a single string by concatenating them.

    There are four ways to concatenate Ruby strings into single string:

    • Using plus sign in between strings.
    • Using a single space in between strings.
    • Using << sign in between strings.
    • Using concat method in between strings.

    Example:

    #!/usr/bin/ruby   
    
      
    
    string = "This is Ruby Tutorial" + " from JavaTpoint." + " Wish you all good luck."   
    
    puts string   
    
      
    
    string = "This is Ruby Tutorial" " from JavaTpoint." " Wish you all good luck."   
    
    puts string   
    
      
    
    string = "This is Ruby Tutorial" << " from JavaTpoint." << " Wish you all good luck."   
    
    puts string   
    
      
    
    string = "This is Ruby Tutorial".concat(" from JavaTpoint.").concat(" Wish you all good luck.")   
    
    puts string

    Output:

    Ruby string 5

    Freezing Strings

    In most programming languages strings are immutable. It means that an existing string can’t be modified, only a new string can be created out of them.

    In Ruby, by default strings are not immutable. To make them immutable, freeze method can be used.

    Example:

    #!/usr/bin/ruby   
    
      
    
    str = "Original string"   
    
    str << " is modified "   
    
    str << "is again modified"   
    
      
    
    puts str   
    
      
    
    str.freeze   
    
      
    
    #str << "And here modification will be failed after using freeze method"

    Output:

    Ruby string 6

    In the above output, we have made the string immutable by using freeze method. Last line is commented as no string can’t be modified any further.

    By uncommenting the last line, we’ll get an error as shown in the below output.

    Output:

    Ruby string 7

    Comparing Strings

    Ruby strings can be compared with three operators:

    • With == operator : Returns true or false
    • With eql? Operator : Returns true or false
    • With casecmp method : Returns 0 if matched or 1 if not matched

    Example:

    
    
    1. #!/usr/bin/ruby   
    2.   
    3. puts "abc" == "abc"   
    4. puts "as ab" == "ab ab"   
    5. puts "23" == "32"   
    6.   
    7. puts "ttt".eql? "ttt"   
    8. puts "12".eql? "12"   
    9.   
    10. puts "Java".casecmp "Java"   
    11. puts "Java".casecmp "java"   
    12. puts "Java".casecmp "ja"  

    Output:

    Ruby string 8


    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *