StrReplace()

Auto Hotkey

StrReplace

Replaces the specified substring with a new string.

OutputVar := StrReplace(Haystack, SearchText , ReplaceText, OutputVarCount, Limit = -1)
Function Example: string := StrReplace(text, 100, 200)

Parameters

OutputVar

The name of the variable in which to store the result of the replacement process.

Haystack

The string whose content is searched and replaced.

SearchText

The string to search for. Matching is not case sensitive unless StringCaseSense has been turned on.

ReplaceText

SearchText will be replaced with this text. If omitted or blank, SearchText will be replaced with blank (empty). In other words, it will be omitted from OutputVar.

OutputVarCount

The unquoted name of a variable in which to store the number of replacements that occurred (0 if none).

Limit

If Limit is 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.

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 SearchText.

Related

RegExReplace, StringCaseSense, InStr, SubStr, StrLen, StrLower, StrUpper

Examples

; Remove all CR+LF's from the clipboard contents:
StrReplace, clipboard, %clipboard%, `r`n

; Replace all spaces with pluses:
StrReplace, NewStr, %OldStr%, %A_SPACE%, +

; Remove all blank lines from the text in a variable:
Loop
{
    StrReplace, MyString, %MyString%, `r`n`r`n, `r`n, ErrorLevel
    if ErrorLevel = 0  ; No more replacements needed.
        break
}