StrReplace() [v1.1.21+]

Replaces the specified substring with a new string.

ReplacedStr := StrReplace(Haystack, Needle , ReplaceText, OutputVarCount, Limit)

Parameters

Haystack
The string whose content is searched and replaced.
Needle
The string to search for. Matching is not case-sensitive unless StringCaseSense has been turned on.
ReplaceText
If blank or omitted, Needle will be replaced with blank (empty), meaning it will be omitted from the return value. Otherwise, specify the string to replace Needle with.
OutputVarCount
If omitted, the corresponding value will not be stored. Otherwise, specify an output variable in which to store the number of replacements that occurred (0 if none).
Limit
If omitted, it defaults to -1, which replaces all occurrences of the pattern found in Haystack. If blank, it defaults to 0. Otherwise, specify the maximum number of replacements to allow. The part of Haystack to the right of the last replacement is left unchanged.

Return Value

This function returns a version of Haystack whose contents have been replaced by the operation. If no replacements are needed, Haystack is returned unaltered.

Remarks

The built-in variables A_Space and A_Tab contain a single space and a single tab character, respectively. They are useful when searching for spaces and tabs alone or at the beginning or end of Needle.

StringReplace, RegExReplace(), InStr(), StringCaseSense, SubStr(), Trim(), StrLen(), StringLower, StringUpper

Examples

Removes all CR-LF pairs from the clipboard contents.

Clipboard := StrReplace(Clipboard, "`r`n")

Replaces all spaces with pluses.

NewStr := StrReplace(OldStr, A_Space, "+")

Removes all blank lines from the text in a variable.

Loop
{
    MyString := StrReplace(MyString, "`r`n`r`n", "`r`n", Count)
    if (Count = 0)  ; No more replacements needed.
        break
}