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.
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.
string
.# 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
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.
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
We can get substrings with a regular expression argument in place of ranges or indexes. The substring returned is the match of the regexp
.
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
testsWe can use an expression for checking if a substring is contained within a string
. We specify the exact substring in square brackets.
string
contains the substring "LATO" so the substring test returns true.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
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).
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
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 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).