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.
With a range, we use periods in between 2 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.
Info For a negative end on a range, we count backwards from the length of the string.
And The period character is used within ranges. We can use 2 or 3 dots to indicate a range.
# 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 4 characters.
first_part = value[0..3]
# Get substring at indexes 2 through 3.
second_part = value[2..3]
# Get substring past index 3 through end of string.
last_part = value[3..-1]
puts first_part
puts second_part
puts last_partAPPL
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).
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_twoOVE
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."
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_lettersace
art
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.
Info In the first example, the philosopher string contains the substring "LATO" so the substring test returns true.
Note When the substring is not found within the string, false is returned. In this way we search strings.
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
endtrue
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.
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 valuecough
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.
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 valuetwo one one
two two one
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).
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 16, 2024 (edit).