Strings in Ruby
In this tutorial, we will learn about strings in Ruby. We will cover the basics of string manipulation, including creating, accessing, modifying, and performing operations on strings.
What is a String
A string in Ruby is a sequence of characters. Strings in Ruby can be created using single quotes, double quotes, or the %q and %Q notations. Strings are used for storing and handling text data.
Creating Strings
Strings can be created in Ruby using single or double quotes:
x = 'Hello, world!'
y = "Hello, world!"Strings can also be created using %q and %Q notations for multi-line strings:
x = %Q{Hello,
world!}
y = %q{Hello,
world!}Example 1: Initializing Strings
In Ruby, we can intiailize a string variable by assigning a string literal to a variable.
For example,
- Create a string variable and initialize it with a value.
- Print the string variable using
putsorp.
Ruby Program
str = 'Hello, world!'
puts strOutput
Hello, world!
Example 2: Accessing Characters in a String
In Ruby, string is a sequence of characters, with a specific index for each character in the string. The index value starts at 0 for the first character of the string, and increments by one for the subsequent characters. We can use this index to access characters, or substring of the given string.
For example,
- Create a string variable and initialize it with a value.
- Access and print individual characters using array indexing or the
[]method.
Ruby Program
str = 'Hello'
puts str[0] # Accessing using array indexing
puts str.slice(1) # using slice methodOutput
H e
Example 3: Modifying Strings
Using the index of the characters, we can modify or update specific characters, or rewrite the string variable with a new value.
For example,
- Create a string variable and initialize it with a value.
- Strings in Ruby are mutable, so you can modify individual characters directly or create new strings based on modifications.
- Print the modified string.
Ruby Program
str = 'Hello'
str[0] = 'J' # Modifying a character
str << ' World!' # Appending new characters
puts strOutput
Jello World!
Example 4: String Concatenation
In Ruby, we can concatenate two or more strings using string concatenation + operator.
For example,
- Create two string variables and initialize them with values.
- Concatenate the strings using the
+operator or theconcatmethod. - Print the concatenated string.
Ruby Program
str1 = 'Hello'
str2 = ' World!'
str3 = str1 + str2 # Concatenating strings
puts str3Output
Hello World!
Example 5: Finding Substrings
In Ruby, we can search for check if a specific search string is present in the given string, or find the index of a given search string in the given string.
To check if a specific search string is present in the given string, we can use include method.
For example,
- Create a string variable and initialize it with a value.
- Use the
indexorinclude?method to find a substring. - Print the position or existence of the substring.
Ruby Program
str = 'Hello, world!'
if str.include?('world')
puts 'Substring found'
else
puts 'Substring not found'
endOutput
Substring found
Example 6: String Length
In Ruby, string length is defined as the number of characters in the string. We can use length method of the string object to find its length.
For example,
- Create a string variable and initialize it with a value.
- Use the
lengthorsizemethod to get the length of the string. - Print the length of the string.
Ruby Program
str = 'Hello, world!'
puts "Length of the string: #{str.length}"Output
Length of the string: 13