Strings
A string is a sequence of one or more characters that represents a word or a sentence. It may contain letters, numbers, or symbols. They are created by surrounding a sequence of characters with single or double quotes. Strings are mutable, which means that they can be changed.
Creating Strings
A string can be created by using the "
(double quotes) or '
(single quotes) around a sequence of characters.
puts 'String created using single quotes.'puts "String created using double quotes."# Output: String created using single quotes.# Output: String created using double quotes.
String Interpolation
Double quotes allow variable interpolation. This means that you can use a variable inside a string.
favorite_color = "blue"puts "My favorite color is #{favorite_color}."# Output: My favorite color is blue.
favorite_color = "blue"puts 'Cannot interpolate #{favorite_color} with single quotes.'# Output: Cannot interpolate #{favorite_color} with single quotes.
Strings are Objects
Strings are objects. They have methods that can be invoked on them.
greeting = "Hello, neighbor"greeting_shout = greeting.upcaseputs greeting_shout# Output: "HELLO, NEIGHBOR"
Accessing Elements within a String
Square brackets ([]
) can be used to access elements within a string by passing in indexes or ranges.
# access element at index 3greeting = "Hello, world!"puts greeting[3]# Output: l# access element at the end of string with negative indexgreeting = "Hello, world!"puts greeting[-1]# Output: !# access a range of elementsgreeting = "Hello, world!"first_word = greeting[0..4]puts first_word# Output: Hello# two comma separated values will indicate the starting index and the number of elements to be accessedgreeting = "Hello, world!"second_word = greeting[7, 5]puts second_word# Output: world
Multi-line Strings
Multi-line strings can be created by using "
, %//
, and <<STRING STRING
syntax.
puts "First string"# Multi-line with `""`puts ""# Multi-line with `%//`puts %/Second string/# Multi-line with `<<STRING STRING`puts <<STRINGIn Ruby, a user can create the multilinestrings easily where into other programminglanguages creating multiline stringsrequires a lot of effortsSTRING
The output would be:
=begin
First string
Second string
Third string
String Replication
Replicating a string is done with the *
operator. The operator is preceded by the string to be replicated and followed by the number of times to replicate the string.
tongue_twister = "Sally sells seashells by the seashore "puts tongue_twister * 5
The output would be:
Sally sells seashells by the seashore Sally sells seashells by the seashore Sally sells seashells by the seashore Sally sells seashells by the seashore
Strings
- .length
- Returns the number of characters in a string.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.