rpartition()
searches a string
based on a specific match. If a match is found, the function will return a string containing the portion before the given match, the match itself, and the part following the match. This method requires a parameter, which is a string
.
str.rpartition(match);
str
: The string that calls the rpartition()
method.
match
: The match we want to find.
The return value is a string that contains three strings. These three include the part before the match
, the match
string, and the part after the match
. Two empty strings and str
are returned if nothing is found.
# create a stringstr = "Edpresso"# call the `rpartition()` methoda = str.rpartition("p")b = str.rpartition("s")c = str.rpartition("a") # no matchputs "#{a}"puts "#{b}"puts "#{c}"
str
."p"
as a match to partition the string we create."b"
as a match to partition the string we create."a"
to partition the string we create.
"a"
is not found in thestr
string.
As we can see, all the partitions were returned except for line 11. Here, two empty strings and str
were returned.