In Euphoria, the patch()
method is used to pad up a sequence to a required length and from a specified position.
To patch a sequence, we have to provide the sequence to be patched, the value we want to pad it with, and specify the index from where we want the patch to begin. Additionally, we’ll have to indicate what we’d use to fill up extra spaces before the padding starts.
The diagram below is a pictorial representation of how patch()
works. It will also replace any character that is at the position it wants to patch.
patch(toBePatched,patchWith,startAt,filler)
toBePatched
: This is the sequence we want to pad up.patchWith
: This will pad up the provided sequence.startAt
: The position to start the sequence patching from.filler
: This is used to pad up extra space or gaps that were not patched. This is possible if the start
value is more than the toBePatched
value. Its default value is " "
if we do not provide it. It is an object data type.This method returns a sliced sequence, such as toBePatched
.
Note: In order to use this method, we have to include the
sequence.e
file in our program, which is part of the standard library of the language.
include std/sequence.e--declare variablessequence toBePatched1, toBePatched2, results1, results2--assigning valuestoBePatched1 = "This sequence will be patch at index10"toBePatched2 = "Work Out"results1 = patch(toBePatched1,"ed wholly at \n",28 )results2 = patch(toBePatched2, "ing ", 5,6)printf(1,"value of result1: %s", {results1})printf(1, "value of result2: %s", {results2})
Line 2 : We include the sequence.e
file from standard library std
.
Line 4: We declare a few varibles of sequence
type.
Lines 8–9: We assign values to some of the declared variables.
Lines 11–12: We assign values of the variables results1
and results2
to the output of the patch()
method on the earlier defined variables.
Lines 14–15: We print the output to the screen.