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]
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]
# Get substring at index 1 with length 2.
middle_two = value[1,2]
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../]
data = "part"
# Get three letter substring for a different string.
three_letters = data[/a../]
arta 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.
Detail In the first example, the philosopher string contains the substring "lato" so the substring test returns true.
Detail 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.
# This substring is not contained with the string.
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"
value = "abcd"
# Change substring starting at index 0 with length 2.# ... Assign a new substring.
value[0,2] = "xy_"
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"
# A regexp also replaces the first (leftmost) instance found.
value[/o\w\w/] = "two"
puts valuetwo one one
twotwo oneo The lowercase letter "o".
\w A word character (letter or digit).
Between, before, after. Suppose we wish to locate substrings that are between, before or after other substrings. We can develop methods with index() to perform this task.