Ruby Strings


Ruby Strings

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!}


Initializing Strings

In Ruby, we can intiailize a string variable by assigning a string literal to a variable.

For example,

  1. Create a string variable and initialize it with a value.
  2. Print the string variable using puts or p.

Ruby Program

str = 'Hello, world!'
puts str

Output

Hello, world!


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,

  1. Create a string variable and initialize it with a value.
  2. 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 method

Output

H
e


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,

  1. Create a string variable and initialize it with a value.
  2. Strings in Ruby are mutable, so you can modify individual characters directly or create new strings based on modifications.
  3. Print the modified string.

Ruby Program

str = 'Hello'
str[0] = 'J' # Modifying a character
str << ' World!' # Appending new characters
puts str

Output

Jello World!


String Concatenation

In Ruby, we can concatenate two or more strings using string concatenation + operator.

For example,

  1. Create two string variables and initialize them with values.
  2. Concatenate the strings using the + operator or the concat method.
  3. Print the concatenated string.

Ruby Program

str1 = 'Hello'
str2 = ' World!'
str3 = str1 + str2 # Concatenating strings
puts str3

Output

Hello World!


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,

  1. Create a string variable and initialize it with a value.
  2. Use the index or include? method to find a substring.
  3. 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'
end

Output

Substring found


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,

  1. Create a string variable and initialize it with a value.
  2. Use the length or size method to get the length of the string.
  3. Print the length of the string.

Ruby Program

str = 'Hello, world!'
puts "Length of the string: #{str.length}"

Output

Length of the string: 13