macaila
asked on
Searching and replacing words from a text box
If i have a textbox with some text in it, how cand i search and replace a word let's say "word" with some other words "word number 2".
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Or the old method:
a = "1234 word 5678"
strToSearch = "word"
strReplaceBy = "word number 2"
start = 1
While InStr(start, a, strToSearch)
a = Mid(a, 1, InStr(start, a, strToSearch) - 1) & strReplaceBy & Mid(a, InStr(start, a, strToSearch) + Len(strToSearch))
start = start + Len(strReplaceBy)
Wend
a = "1234 word 5678"
strToSearch = "word"
strReplaceBy = "word number 2"
start = 1
While InStr(start, a, strToSearch)
a = Mid(a, 1, InStr(start, a, strToSearch) - 1) & strReplaceBy & Mid(a, InStr(start, a, strToSearch) + Len(strToSearch))
start = start + Len(strReplaceBy)
Wend
Here you go.
Private Sub Command1_Click()
Text1.Text = Replace(Text1.Text, "word", _
"word number 2")
Text1.Text = Replace(Text1.Text, "word", _
"word")
Text1.SelStart = Len(Text1.Text)
End Sub
Private Sub Command1_Click()
Text1.Text = Replace(Text1.Text, "word", _
"word number 2")
Text1.Text = Replace(Text1.Text, "word", _
"word")
Text1.SelStart = Len(Text1.Text)
End Sub
OK, i think im missing something here ! What is wrong with bobbit31's post. It's the best answer that fits this question, why make it so complex ?
lol, my thoughts exactly ;)
Some like it complex.
And it is only with an educational purpose.
And it is only with an educational purpose.
ASKER
I'll accept one answer as soon as I get VB6 I use VB4 so I get error messages.
ASKER
Well bobbit31 was the first person who answered me and i try not to make my programs complex because that would mean they wouldn't run on older computers...
so why the 'B' grade?
x=1
'z is length of "word", in this case 4'
z= 4
do until mid(textbox1.text,x,z) = "word"
x=1+x
loop
textbox1.text = left(textbox1.text,x) & "word number 2" & right(textbox1.ext,x+z)