Updated April 3, 2023
Definition of Ruby Strings
A combination of the sequence of one or many characters in Ruby is called a string, a string can be a combination of letters as well as numbers and symbols. in Ruby string is an object and it can be mutable means we can change the string, if we wanted to create any string then we need to put the sequence of character inside the single quotes or by putting characters sequences inside the double quotes, we do not require to manually defining of the datatype of the variable by using single or double quote Ruby automatically understand it is string type.
How to Define and Initialize Strings in Ruby?
Below is the simple syntax for the definition and initialization for the String in Ruby. We can explain the below syntax in the following way.
- We have given the name of the variable as the Str, you can give any other name.
- Str initially assigned the black string value. By assigning the blank value with using a double quotation the Ruby compiler automatically understood that this is the string.
- In the next line we have assigned the value “sequence of characters’ ‘, we have already discussed that string in Ruby is Immutable hence here the initial value of Str variable was blank string and after that, we assigned the other value for it.
Str =""
Str ="sequence of characters"
Examples of Strings Operations in Ruby
There are many string operations which can be very useful for the day to day development related to the string in Ruby. Let us understand string operations.
1. Accessing
To access any letter or a particular position in a string is very easy, we use the [] symbol for accessing, they behave just like array indexes. We can get the value by using the index also. In the below example we are trying to access any attribute from the string variable strAccess. We are able to access letters directly with the index of those letters.
Code:
strAccess = "Welcome to Ruby String concepts and enjoy the tutorial"
puts "The string output is #{strAccess["Wel"]}"
puts "The string output is #{strAccess['Ruby']}"
puts "The string output is #{strAccess[4]}"
puts "The string output is #{strAccess[-4]}"
puts "The string output is #{strAccess[12, 11]}"
puts "The string output is #{strAccess[14 .. 17]}"
Output:
2. Concatenate
This is a way to add to string with each other. Many times we may need to add two strings with each other so that we can get a unique output. In the below example we are doing this with three different mechanisms, in the first mechanism we are adding with using pre adding technique (+=), in the second way we are using the method of ruby cocate, concat method adds two string and will return one string after concatenation of the strings. We can use either + or method defined by the Ruby for concatenation.
Code:
variable1 = ""
variable1 += "My name is Raj"
variable1 += " I am from Mumbai"
variable1 += " I love Coding"
puts variable1
variable2 = ""
variable2.concat("Raj")
variable2.concat("Kumar")
puts variable2
variable3=23
variable4="Raj"
puts variable3.to_s+variable4
Output:
3. Compare
A comparison of string programming is one of the crucial and important jobs. There are two ways to compare in Ruby: one is using the “=” operator and another way is by using the eql method of a ruby. In both case we need two string for comparison. These comparisons are case sensitive which means if the case matches then only true. In the below example we are comparing “Raj” with “raj” which returns false as the first letter is not in the same case.
Code:
puts "Raj".eql?("raj")
puts "String".eql?("String")
puts "Raj"=="Raj"
puts "Raj"=="raj"
Output:
4. Freeze
This feature will be to manage data consistency through the whole flow of the application. It allows us to fix the value of any variable containing string by using the method Freeze. In the below example initially, we assign some value to the variable msg and in the next line we are trying to replace the value with other string, but it will return an error as it will not allow us to replace the initial value which we have assigned to the variable. Even we cannot replace the index or any letter from the origin string if once we have implemented Freeze.
Code:
msg = "Hello friend, my name is Raj"
msg.freeze #Here we are freezing the string and it cannot modify now
msg.replace("ello friend, my name is Vijay") #It will return an error
puts msg[0]
msg[0] = 'W' #It will return an error
Output:
5. Replication
In case if we need to repeat the same string many times then we can use the replication. To use the replication we need to use the symbol (*).If you know the multiplication, it works in a similar way, we just have to use * with the number of times we wanted to have the same string. Never confuse with numeric multiplication, as in that case, it works as multiplication, only in case of string it changes its behavior.
Code:
msg = "This is world is a beautiful place\n"
# Here we are using * operator to create number same string
puts msg * 5
Output:
6. Interpolation
Interpolation is a way of merging string and variables together, in the below example we are merging the variables along with the string and printing them. Please see the below example along with the screen of the output.
Code:
location ="Mumbai"
age = 25
name = "Raj"
puts "Hello friends ,my name is #{name} & my age is #{age} years old .I am from #{location}"
Output:
Conclusion
From this tutorial, we learned about Ruby Strings and we learned about Ruby Strings uses and its various important operations with the help of the examples. We focused on some of the important Ruby Strings operations with their definitions.
Recommended Articles
We hope that this EDUCBA information on “Ruby Strings” was beneficial to you. You can view EDUCBA’s recommended articles for more information.