Scripting languages such as javascript and PHP allow a lot of flexibility while working with arrays. This is because these languages have built-in methods to manipulate an array. In Euphoria, the sequence is used as an array of integers or strings. One such manipulation carried out on a sequence is searching.
In this shot, we’ll learn a sequence search method called match_replace()
.
search_replace()
method?The search_replace()
is a built-in method of the search.e
file in the standard library. It performs a search on a sequence and replaces the character that has been indicated with another character.
match_replace(to_be_replaced, sequence_to_replace_in,replace_with,max_occurrence)
All the parameters are mandatory.
to_be_replaced
: This parameter indicates the substring, which we wish to replace with another:replace_with
inside the sequence.
sequence_to_replace_in
: This is the sequence where the substring, to_be_replaced
, could be found and replaced.
replace_with
: This is the substring with which we want to exchange the parameter to_be_replaced
.
max_occurrence
: This is an integer value, which indicates the maximum occurrence of the parameter to_be_replaced
. If this parameter is 0
or less than zero, all occurrences of to_be_replaced
in the parameter sequence_to_replace_in
will be exchanged.
This function returns a sequence, which is the manipulated sequence.
Note: To use this function, we must include the
search.e
file in our program code:
include std/search.e
include std/search.e--declare some variablessequence to_be_replaced, sequence_to_replace_in, replace_withsequence outcomeatom max_occurrence = 0--assign values to variablesto_be_replaced = "try"sequence_to_replace_in = "try, if you fall try again, just don't stop to try"replace_with = "rise"--use the replace functionoutcome = match_replace(to_be_replaced,sequence_to_replace_in,replace_with,max_occurrence)--display output to screenputs(1,match_replace("Flash", "Flash the Flash Light \n","FLASH",1))printf(1, "%s", {outcome})
match_replace()
function and provide the four required parameters.