Find and replace

Syntax

int s.findreplace(findwhat [replaceto] [flags] [delim] [from])

 

Parameters

s - str variable.

findwhat - string to find.

replaceto - replacement string. Default: "".

flags:

1 case insensitive.
2 whole word.
4 single replacement.
8 repeat replacement until s will not contain substrings that match findwhat. Without this flag, the result string can contain new such substrings. For example, if in string "abbcc" is replaced "bc" to "", the result is "abc". With this flag, the result is "a".
16 replace with [0] character. replaceto is not used and can be "".
32 replace [0] character (QM 2.2.0). findwhat is not used and can be "".
64 QM 2.3.3. Match always if findwhat begins/ends with a delimiter. Can be used with flag 2.
0x100 delim is table of delimiters.
0x200 QM 2.3.3. Add blanks to delim.

delim - delimiters.

from - start from here. 0-based character index in s. Default: 0.

 

Remarks

Searches in s for findwhat, and replaces with replaceto. If flag 2 is set, searches for substrings delimited by characters in delim, else delim is ignored.

 

If flag 4 is not set, replaces all found findwhat and returns the number of replacements. If flag 4 is set, replaces only the first found findwhat, and returns 0-based character index of findwhat in s.

 

Examples

str s="one two one twoo one.two"
s.findreplace("two" "THREE" 2 " .")
 now s is "one THREE one twoo one.THREE"
 
s="one two one two one two"
s.findreplace("two" "THREE" 4 "" 5)
 now s is "one two one THREE one two"
 
 remove empty lines:
s="one[][][][][]two"
s.findreplace("[][]" "[]" 8)