Ruby String Operations


title: Ruby String Operations

Both concatenation and multiplication can be performed on strings.

Concatenation:

  • Strings can be joined together using any of the following methods:
    • + operator
    • << operator
    • .concat method
    "Hello" + " World" + "!" #=> Hello World! "Hello" << " World!" #=> Hello World! string1 = "Hello" string2 = " World!" string1.concat(string2) #=> Hello World!

Multiplication:

  • Strings can be multiplied by an integer value using the * operator.
    ruby "Hello" * 3 #=> HelloHelloHello

Replacing a substring

  •   We can search for sub-strings or use Regex for searching and replacing character within a string.
    ruby "Hey mom, look at this string".sub('mom', 'dad') #=> Hey dad, look at this string

Comparison:

  • Strings can be compared, returns -1, 0, +1 or nil depending on whether string is less than, equal to, or greater than other_string.
"abcdef" <=> "abcde" #=> 1 "abcdef" <=> "abcdef" #=> 0 "abcdef" <=> "abcdefg" #=> -1 "abcdef" <=> "ABCDEF" &nbsp; #=> 1

This article needs improvement. You can help improve this article. You can also write similar articles and help the community.