What is the replace() method in Euphoria?
Overview
The replace() method is used to make changes to the content of a sequence so as to get our desired output.
The replace() method is part of Euphoria’s core library. This method will slice a target sequence starting from an indicated start_point to another end_point and replace the sliced section with another set of replace_with values. This will produce the transformed sequence, which has the new value added to it.
Unlike the match_replace() method, which looks for a match to replace, the replace() method won’t look for a match. Rather, it replaces whatever is in the indicated position and replaces it with whatever is available.
Syntax
replace(target,replace_with,start_point,endpoint)
Parameters
-
target: This is a required parameter. It is a sequence that will be sliced, and the sliced portion will be replaced with a value. -
replace_with: This is also a required parameter. It is an object data type that will be used to replace the slice portion in the target. -
start_point: This is an integer that indicates the index position in the target where the slicing will begin. It is required as well. -
endpoint: This integer parameter, if omitted, will be assumed by the parser to be the same as thestart_point, therefore, causing only a single value to be replaced in the sequence. This parameter indicates where the slice is to end.
Return value
It returns a sequence that contains the target with the items in the start_point and end_point slice removed and replaced by the replace_with value.
Example
--declare variablessequence target = "This is the Edpresso system for byte sized shots"object replace_with = ", HOME OF"integer start_point = 21integer end_point = 31--replace the indicated slicesequence output = replace(target,replace_with,start_point,end_point)printf(1,"%s",{output})
Explanation
- Lines 3–6: We declare the variables that will be used throughout the code.
- Line 9: We use the
replace()method to do the slicing and replacement. - Line 10: We print the output from the method to the screen.