StrReplace

Replaces the specified substring with a new string.

NewStr := StrReplace(Haystack, SearchText , ReplaceText, CaseSense, OutputVarCount, Limit := -1)

Parameters

Haystack

Type: String

The string whose content is searched and replaced.

SearchText

Type: String

The string to search for.

ReplaceText

Type: String

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 the return value.

CaseSense

Type: Integer or String

One of the following values (defaults to 0 if omitted):

"On" or 1 (True): The search is case sensitive.

"Off" or 0 (False): The letters A-Z are considered identical to their lowercase counterparts.

"Locale": The search is case insensitive according to the rules of the current user's locale. For example, most English and Western European locales treat not only the letters A-Z as identical to their lowercase counterparts, but also non-ASCII letters like Ä and Ü as identical to theirs. Locale is 1 to 8 times slower than Off depending on the nature of the strings being compared.

OutputVarCount

Type: Variable

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

Limit

Type: Integer

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.

Return Value

Type: String

This function returns the replaced version of the specified string.

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, InStr, SubStr, StrLen, StrLower, StrUpper

Examples

#1

; Remove all CR+LF's from the clipboard contents:
A_Clipboard := StrReplace(A_Clipboard, "`r`n")

; Replace all spaces with pluses:
NewStr := StrReplace(OldStr, A_Space, "+")

#2: Remove all blank lines from the text in a variable:

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