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
Needle will be replaced with this text. If omitted or blank, Needle will be replaced with blank (empty). In other words, it will be omitted from the return value.
OutputVarCount
Specify a 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. 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's 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
}