The Replace function replaces individual characters
with other individual characters.
The replacement is done at the individual character level
and not the string level. Every occurrence of a character is replaced
with specified replacement character as shown in the following example:
- list of characters to be replaced is equal to 'abc'
- list or replacement characters is equal to 'ABC'
- input string is equal to 'aWWWbYYYYcPPPPabc'
The result string is equal to ''AWWWBYYYYCPPPPABC' because
the replacement is done at the character level - every character is
replaced. (If the replacement was done at the string level the resulting
string would be equal to 'aWWWbYYYYcPPPPABC'.)
Parameter: input
The string to search for
characters to replace. The value for this parameter can be provided
by a source node, the result of another function, or a value you specify.
Parameter: fromChars
A list of the character
or characters to be replaced. If you need to replace several characters,
they should be listed without any separation. The value for this parameter
can be provided by a source node, the result of another function,
or a value you specify.
Parameter: toChars
A list of character or
characters to use as replacements. List the replacement characters
in this parameter in the same order as characters to be replaced in
the
fromChars parameter because the first character
from the
fromChars parameter is replaced by the
first character in
toChars parameter and so on.
For more information, see Example 2. The value for this parameter
can be provided by a source node, the result of another function,
or a value you specify.
Note: You can not directly replace a single
character with an empty string because the toChars parameter
cannot equal a empty string - a string with no characters. For a workaround,
see Example 3.
Returns: string
The result of replacing all
occurrences of characters in the second parameter (fromChars)
that are found in the first parameter (input)
with the matching characters in the third parameter (toChars).
Example 1
This example shows a simple one-to-one
character replacement as shown in the following figure:
The run-time result of running this function
is the following string:
A list of chArActers
All
lowercase a characters are replace with uppercase A characters.
Note: In
this example, the input parameter is hardcoded to a specific value.
Typically this value is supplied by a source node in a map.
Example 2
In this example, the first character
from the
fromChars parameter is replaced by the
first character of the
toChars parameter, the
second character from the
fromChars parameter
is replaced by the second character of the
toChars parameter,
and so on. The replacement of multiple characters is shown in the
following figure:
In
this example, the non-numeric characters (specified at the end of
the fromChars parameter) are removed from the
phone number string. All the numeric characters are replaced with
the same numeric character; for example, the 1 character is replaced
by the 1 character. The other non-numeric characters at the end of
the fromChars do not have a replacement character
specified in the toChars, so they are removed
from the returned string.
The run-time result
of running this function is the following string:
000238882349940 Note: In
this example, the input parameter is hardcoded to a specific value.
Typically this value is supplied by a source node in a map.
Example 3
This example shows a workaround
to the limitation that you cannot replace a single character with
an empty string. This limitation exists because the
toChars parameter
cannot be set to an empty string - a string with no characters. You
can workaround this limitation by supplying a dummy character like
a hyphen (-) that is not found in the
input parameter.
For example, you could specify the following values for the parameters
of the
Replace function as shown in the following figure:
In this example, the input parameter is equal to '1 2
3' (1, space, 2, space, 3), the fromChars parameter
is equal to '- ' (hyphen, space), and the toChars parameter
is equal to '-' (hyphen). During run time, the Replace function
with these values strips the spaces from input parameter and the run-time
result is the following string:
123
Attention: When you specify the strings in the Value field
of the Function Properties dialog box, do not enter the quotes. Enter
just the hyphen and the space and not the quote characters.
Note: In
this example, the input parameter is hardcoded to a specific value.
Typically this value is supplied by a source node in a map.