I'm fully aware that this thread is quite old now (I found it on google), but I wanted to post something here:
Here is my own search and replace function(s) for strings.
Just read in a textfile and read every line as a string (and modify that string, then replace the line in that text file with the new string), until the end of file, using these two functions together to do the replacements.
' My own equivalent of VB.NET's inbuilt "instr" function. Because I can
Function InString(ByRef sMas As String, ByRef sDum As String) As Integer
For i As Integer = 1 To sMas.Length Step 1
If Mid(sMas, i, sDum.Length) = sDum Then
Return i
End If
Next
Return 0
End Function
Function SearchAndReplace(ByRef sMas As String, ByRef sDum As String, ByRef sRep As String) As String
Do Until InString(sMas, sDum) = 0 Or InString(sRep, sDum) > 0
For i As Integer = 1 To sMas.Length Step 1
If Mid(sMas.ToLower, i, sDum.ToLower.Length) = sDum.ToLower Then
sMas = (Mid(sMas, 1, i - 1) + sRep + Mid(sMas, i + sDum.Length, sMas.Length - i + 1 + sDum.Length))
Exit For
End If
Next
Loop
Return sMas
End Function
It's only flaw is when the searchitem to replace is also contained in the actual replacement:
e.g.
Call System.Console.WriteLine(SearchAndReplace("redundant box is redundant", "redundant", "non-volatile redundant, hairy, and by all rights awesome"))
Will still return "redundant box is redundant", but what I would ideally want it to return is "non-volatile redundant, hairy, and by all rights awesome box is non-volatile redundant, hairy, and by all rights awesome".
It's a flaw that I would like to fix, but I haven't come up with a solution yet (which means no good search-and-replace in my text editor yet).