StrClean

Removes or replaces characters in a string.

Syntax:

StrClean( source-string, characters, replacement, match-case, mode)

Parameters:

(s) source-string the string to be operated upon.

(s) characters a string that specifies the characters in "source-string" to be replaced or retained.

(s) replacement the replacement string to be inserted in place of matching (or non-matching) characters.

(i) match-case @TRUE or @FALSE, indicating whether string comparisons are case-sensitive or case-insensitive, respectively.

(i) mode see below.

Returns:

(s) the new string, with all changes made. The original string ("source-string") is untouched.

 

"characters" is a string that specifies the characters in "source-string" to be replaced or retained. This parameter can be a single character (eg, "x"), a list of characters (eg, "~!@#"), or a blank string ("").

"replacement" specifies the replacement string to be inserted in place of matching (or non-matching) characters. It can be a string, one or more characters long. It can also be a blank string (""), in which case matching characters are removed.

"mode" can be one of the following:

Value

 Meaning

1

Replace all occurrences of "characters" with "replacement"

2

Replace all non-occurrences of "characters" with "replacement"

 

This function can be used to remove or replace specific characters in a string, or to retain only specific characters in a string.

If "mode" is 1, then any characters which appear in "characters" will be replaced with "replacement". If "replacement" is a blank string, then any characters which appear in "characters" will be removed. If "characters" is a blank string, then no changes will be made.

If "mode" is 2, then any characters which DON'T appear in "characters" will be replaced with "replacement". If "replacement" is a blank string, then any characters which DON'T appear in "characters" will be removed. If "characters" is a blank string, then ALL characters will be replaced.

Example:

; Remove all spaces from a string
newstring = StrClean("Have a nice day", " ", "", @FALSE, 1)
Message("StrClean", newstring)
; Replace all ampersands with the HTML code "&"
newstring = StrClean("Here & there & everywhere", "&", "&", @FALSE, 1)
Message("StrClean", newstring)
; Remove all characters other then letters and spaces
newstring = StrClean("Healthy, wealthy, & wise.", "abcdefghijklmnopqrstuvwxyz ", "", @FALSE, 2)
Message("StrClean", newstring)

See Also:

StrInsert, StrOverlay, StrReplace, ItemReplace