Replace+substring

A function to replace all instances of a substring (character) in a string with another substring.

It could be useful, e.g, if you want to remove line breaks / carriage returns from texteditor contents (replace chr$(13) with " " to keep the space - and chr$(10) with an empty string).

Parameters: replaceString$(string$, replaceWhat$, replaceWith$)

code format="qbasic" string$(0) = "Line 1"; chr$(10); "Line 2"; chr$(10); "Line 3" from$(0) = chr$(10) : to$(0) = " " string$(1) = "Hello Mary! Hello John!" from$(1) = "Hello" : to$(1) = "Bonjour" string$(2) = "abracadabra" from$(2) = "a" : to$(2) = "abracadabra" print "Function replaceString$(:" print tab(5); "Replaces all instances of substring" print tab(5); "(character) with another substring in" print tab(5); "a given string." print print "Examples:" for i = 0 to 2   print tab$(5); "Unchanged: "; string$(i)    print tab$(5); "Changed: "; replaceString$(string$(i), from$(i), to$(i)) next i end

'**************************************************************************************************** function replaceString$(replaceString$, a$, b$) i = len(a$) : j = len(b$) while instr(replaceString$, a$, a) > 0 c = instr(replaceString$, a$, a)       replaceString$ = left$(replaceString$, c-1); b$; mid$(replaceString$, c+i) a = c+j wend end function code