# 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".
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
textbox1.text = Replace(textbox1.text, "word", "word number 2")
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
if you know what the word is, and it's length, you could do something like the following

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)
0
Commented:
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
0
Commented:
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
0
Commented:
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 ?
0
Commented:
lol, my thoughts exactly ;)
0
Commented:
Some like it complex.
And it is only with an educational purpose.
0
Author Commented:
I'll accept one answer as soon as I get VB6 I use VB4 so I get error messages.
0
Author Commented:
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...
0
Commented: