Ruby Substring Examples
Get substrings with ranges and indexes. Regular expressions can be used to get substrings.Substring. A substring is a range of characters within an existing string. There is no substring method in Ruby. Instead we rely upon ranges and expressions.
Ranges and indexes. With a range, we use periods in between two numbers—the first and last index of the substring. With indexes, we use a comma between 2 numbers—a start and a count.
Range example. We begin with ranges. We first examine the string "apple," which has 5 chars numbered 0 through 4. With the first range, we get a substring of the first 4 chars.
Negative: For a negative end on a range, we count backwards from the length of the string.
Dots: The period character is used within ranges. We can use 2 or 3 periods. If you enjoy typing periods, 3 is better.
Tip: The colon is not supported within ranges—we must use period-ranges. Otherwise the syntax resembles Python.
Ruby program that uses substring
# Index 0 = a
# Index 1 = p
# Index 2 = p
# Index 3 = l
# Index 4 = e
value = "apple"
# Get substring at indexes 0 through 3.
# ... This is the first four characters.
first_part = value[0..3]
# Get substring at indexes 3 through 4.
# ... This is the character at index 3.
second_part = value[2..3]
# Get substring past index three through end of string.
last_part = value[3..-1]
puts first_part
puts second_part
puts last_part
Output
appl
pl
le
Comma indexes example. This is another syntax for substrings. We specify 2 numbers separated by a comma. The first number is the start index. The second is the length.
Tip: With the comma substring syntax, we do not specify a range with two indexes. The second number is now a length (a character count).
Ruby program that uses substring comma syntax
value = "love"
# Get substring at index 1 with length 3.
# ... First argument is start index, second is length.
last_three = value[1,3]
puts last_three
# Get substring at index 1 with length 2.
middle_two = value[1,2]
puts middle_two
Output
ove
ov
Regular expressions. We can get substrings with a regular expression argument in place of ranges or indexes. The substring returned is the match of the regexp.
Here: We specify that we want substrings that are three characters long and begin with the letter "a."
Result: The first substring expression returns "ace" which is found in "peace." The second returns "art."
Ruby program that uses regexp substrings
data = "peace"
# Get three-letter substring starting with lowercase "a."
three_letters = data[/a../]
puts three_letters
data = "part"
# Get three letter substring for a different string.
three_letters = data[/a../]
puts three_letters
Output
ace
art
Regexp pattern
a The lowercase letter "a".
. Any character.
Substring tests. We can use an expression for checking if a substring is contained within a string. We specify the exact substring in square brackets.
True: In the first example, the philosopher string contains the substring "lato" so the substring test returns true.
False: When the substring is not found within the string, false is returned. In this way we search strings.
Ruby program that uses substring test expression
philosopher = "plato"
# This substring is found, so the expression returns true.
if philosopher["lato"]
puts true
end
# This substring is not contained with the string.
if philosopher["soc"]
puts false
end
Output
true
Assign. A part of a string can be changed by assigning a substring. We can assign to a range of a string, or specify a start and a length (separated by a comma).
Tip: Regexp and exact strings can also be used. The matching part of the string is replaced with the specified value.
Ruby program that changes substrings
value = "coffee"
# Change range from 2 to last index.
# ... This replaces the substring.
value[2..-1] = "ugh"
puts value
value = "abcd"
# Change substring starting at index 0 with length 2.
# ... Assign a new substring.
value[0,2] = "xy_"
puts value
Output
cough
xy_cd
Assign, first match. In assigning a substring, only the first match is replaced. Another string method, like replace(), can be used to replace all matching instances.
Ruby program that assigns substrings
value = "one one one"
# Replace first instance of this substring.
value["one"] = "two"
puts value
# A regexp also replaces the first (leftmost) instance found.
value[/o\w\w/] = "two"
puts value
Output
two one one
two two one
Regexp pattern
o The lowercase letter "o".
\w A word character (letter or digit).
Assign all instances. We must use the gsub method to replace all instances of a substring with another. The sub() method acts in the same way as assigning a substring.
Sub, gsub
A review. A single character can be returned with a single number. A longer substring can be specified with a simple range or 2 indexes (a start and a count).
With more advanced expressions, though, like regexp, we can encode searching within a substring expression. This is a powerful yet concise way of extracting string data.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.