Ruby | String Basics (original) (raw)

Last Updated : 30 Jul, 2018

In Ruby, string is a sequence of one or more characters. It may consist of numbers, letters, or symbols. Here strings are the objects, and apart from other languages, strings are mutable, i.e. strings can be changed in place instead of creating new strings. String's object holds and manipulates an arbitrary sequence of the bytes that commonly represents a sequence of characters. Creating Strings: To create the string, just put the sequence of characters either in double quotes or single quotes. Also, the user can store the string into some variable. In Ruby, there is no need to specify the data type of the variable. Example:

Ruby `

Ruby program to demonstrate

the creation of strings

using single quotes

puts 'Ruby String using single quotes'

using double quotes

puts "Ruby String using double quotes"

storing string into variables

str1 = "GFG" str2 = 'Geeks'

displaying string

puts str1 puts str2

`

Output:

Ruby String using single quotes Ruby String using double quotes GFG Geeks

Note: The only difference between using single and double quotes is that the double quotes will interpolates the variables but single quotes can't interpolate.Example:

Ruby `

Ruby program to demonstrate the difference

while using single and double quotes to

create strings

storing string into variables

str1 = "GFG" str2 = 'Geeks'

using single quotes

puts 'Cannot Interpolate str1: #{str1}'

using double quotes

puts "Interpolating str2: #{str2}"

`

Output:

Cannot Interpolate str1: #{str1} Interpolating str2: Geeks

Strings are objects: As you know that Ruby is an object-oriented language so string in Ruby are objects. Basically, an object is a combination of the data and methods which enhance the communication property.Example:

Ruby `

Ruby program to illustrate that

string are objects in Ruby

#!/usr/bin/ruby

using double quotes

str = "GeeksforGeeks"

puts str

using new method to create string

object and assigning value to it

str2 = String.new "GeeksforGeeks"

puts str2

`

Output:

GeeksforGeeks GeeksforGeeks

Access String Elements: User can access the string elements by using the square brackets []. In square brackets [], the user can pass the strings, ranges or indexes. Syntax:

name_of_string_variable[arguments]

Example:

Ruby `

Ruby program to illustrate the

accessing of string

#!/usr/bin/ruby

storing string in variable

str = "GeeksforGeeks Sudo Placements"

accessing the specified substring

puts str["Geeks"] puts str['for']

passing index as an argument which returns

the specified character

puts str[3]

passing the negative index as an argument which

returns the specified character from the

last of the string

puts str[-3]

passing Two arguments which are separated

by a comma that returns characters starting

from the 1st index and the 2nd index is the

number of characters

puts str[14, 10]

using range operators in passed arguments

puts str[14 .. 17]

`

Output:

Geeks for k n Sudo Place Sudo

Creating Multiline Strings: In Ruby, a user can create the multiline strings easily whereas in other programming languages creating multiline strings requires a lot of efforts. There are three ways to create multiline strings in Ruby as follows:

  1. Using Double Quotes("") It is the simplest way to create the multiline strings by just putting the string between the quotes. Between double quotes, the user can add the newline character and so on.
  2. Using (%/ /) To create the multiline string just put the string between the %/ and /.
  3. Using (<< STRING STRING) To create the multiline string just put the string between the << STRING and STRING. Here STRING should be in capital letters.

Example:

Ruby `

Ruby program to illustrate the

multiline strings

#!/usr/bin/ruby

Using Double Quotes

puts "In Ruby, a user can create the multiline strings easily where in other programming languages creating multiline strings requires a lot of efforts"

puts ""

Using %/ /

puts %/ In Ruby, a user can create the multiline strings easily where into other programming languages creating multiline strings requires a lot of efforts/

puts ""

Using <<STRING STRING

puts <<STRING

In Ruby, a user can create the multiline strings easily where into other programming languages creating multiline strings requires a lot of efforts STRING

`

Output:

In Ruby, a user can create the multiline strings easily where in other programming languages creating multiline strings requires a lot of efforts

In Ruby, a user can create the multiline strings easily where into other programming languages creating multiline strings requires a lot of efforts

In Ruby, a user can create the multiline strings easily where into other programming languages creating multiline strings requires a lot of efforts

String Replication: Sometimes a user may require to repeat some sort of string multiple times. So to make the replication of string in Ruby, make the use of (*) operator. This operator is preceded by the string to be replicated and followed by the number of times to make replicas.Syntax:

string_variable_or_string * number_of_times

Example:

Ruby `

Ruby program to illustrate the

replication of strings

#!/usr/bin/ruby

string to be replicate

str = "GeeksForGeeks\n"

using * operator

puts str * 7

`

Output:

GeeksForGeeks GeeksForGeeks GeeksForGeeks GeeksForGeeks GeeksForGeeks GeeksForGeeks GeeksForGeeks