Replace or remove character(s) in a string.
{
global replaceCharacterFunc do={
while condition=[find $1 $2 0] do={
set $1 ("$[pick $1 0 ([find $1 $2]) ]".$3."$[pick $1 ([find $1 $2]+1) ([len $1])]")}
return $1
}
# Replace a character with an other character
put [$replaceCharacterFunc "Can-t-be-used-as-name-for-variable" "-" "*"]
# Remove unwanted character(s)
put [$replaceCharacterFunc "Can-t-be-used-as-name-for-variable" "-"]
}
Correct that the first character in the string could also be the one that could be replaced:
while condition=[find $1 $2 0] do={
Update: added matching on multiple chars and replace those with one or more chars.
{
global mreplaceCharFunc do={
:local n [:len $2]
while condition=[find $1 $2 0] do={
set $1 ("$[pick $1 0 ([find $1 $2]) ]".$3."$[pick $1 ([find $1 $2]+$n) ([len $1])]")}
return $1
}
}
:set $a [$mreplaceCharFunc "this @@@ @@ @ test of replacing multiple char(s)" @@ is]; :put $a
this is@ is @ test of replacing multiple char(s)