StrReplace() [v1.1.21+]

Replaces the specified substring with a new string.

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


The string whose content is searched and replaced.
The string to search for. Matching is not case sensitive unless StringCaseSense has been turned on.
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.
Specify a output variable in which to store the number of replacements that occurred (0 if none).
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.


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


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.

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