|
-
Oct 19th, 2008, 03:30 PM
#1
Thread Starter
Member
[RESOLVED] Replacing a word in a sentence
When I run the program with the following:
sent="What you don't know won't hurt you"
word="know"
replace="owe"
the result is: What you don't owew won't hurt you
How can I get it to replace 'know' and not just 'kno' ?
Thanks,
Levells
Code:
Private Sub cmdCompute_Click()
Dim sent, word, replace As String
sent = txtSent.Text
word = txtWord.Text
replace = txtReplace.Text
picbox.Cls
picbox.Print "The original sentence was: "; sent
picbox.Print "You replace "; word; " with "; replace; " and the result is:"
Mid(sent, InStr(sent, word), Len(txtWord.Text)) = replace
picbox.Print sent
End Sub
I use VB 6. I've been programming since September 2008.
-
Oct 19th, 2008, 04:01 PM
#2
Re: Replacing a word in a sentence
Use Replace() function - it's very simple to use so I' won't be posting any samples for you.
-
Oct 19th, 2008, 04:05 PM
#3
Re: Replacing a word in a sentence
 Originally Posted by Levells
When I run the program with the following:
sent="What you don't know won't hurt you"
word="know"
replace="owe"
the result is: What you don't owew won't hurt you
How can I get it to replace 'know' and not just 'kno' ?
Thanks,
Levells
Code:
Private Sub cmdCompute_Click()
Dim sent, word, replace As String
sent = txtSent.Text
word = txtWord.Text
replace = txtReplace.Text
picbox.Cls
picbox.Print "The original sentence was: "; sent
picbox.Print "You replace "; word; " with "; replace; " and the result is:"
Mid(sent, InStr(sent, word), Len(txtWord.Text)) = replace
picbox.Print sent
End Sub
Hi Levells,
You should get into the habit of adding prefixes to your variables.
Not only does it make it easier to tell what your program is doing, it also helps prevent conflicts.
In this case 'replace' is another reserved VB command word, just like Day was in one of your earlier posts.
-
Oct 19th, 2008, 04:26 PM
#4
Re: Replacing a word in a sentence
 Originally Posted by longwolf
Hi Levells,
You should get into the habit of adding prefixes to your variables...
Absolutely unnecessary technique, however trying to give your variables some more meaningfull names is definitely a good idea:
Code:
Dim WordSent As String
Dim WordReceived As String
Dim WordReplace As String
Also, the following declaration makes only the "replace" variable as String, others are left as Variant - that's how VB6 was designed.
Code:
Dim sent, word, replace As String
This changed since the introduction of VB.Net back in 2001.
-
Oct 19th, 2008, 05:36 PM
#5
Thread Starter
Member
Re: Replacing a word in a sentence
 Originally Posted by RhinoBull
Use Replace() function - it's very simple to use so I' won't be posting any samples for you. 
Was quite easy, thanks for pointing it out as the only notes I have mention Mid.
I use VB 6. I've been programming since September 2008.
-
Oct 19th, 2008, 06:03 PM
#6
Re: Replacing a word in a sentence
 Originally Posted by longwolf
In this case 'replace' is another reserved VB command word
Replace is not a reserved word. It is however a VBA function, but that doesn't make it reserved. You can declare a variable with that name if you wish, however if you like to use the function in the same procedure/module that the variable is declared in you must call it using this syntax:This doesn't mean that I suggest that you use variable names that are the same or even very simular to usual function names in the language. I'm just stating the fact that there is a difference between a reserved word and a library function name.
-
Oct 19th, 2008, 08:29 PM
#7
Re: [RESOLVED] Replacing a word in a sentence
So are you guys saying that prefixes are a bad idea and that they wouldn't help him avoid the conflicts he keeps having?
-
Oct 19th, 2008, 09:07 PM
#8
Re: [RESOLVED] Replacing a word in a sentence
No, I'm just saying that Replace is not a reserved word
-
Oct 19th, 2008, 09:32 PM
#9
Re: [RESOLVED] Replacing a word in a sentence
 Originally Posted by longwolf
So are you guys saying that prefixes are a bad idea ... ?
No, I said "unnecessary" - read post #4.
-
Oct 19th, 2008, 09:39 PM
#10
Re: [RESOLVED] Replacing a word in a sentence
Actually you said 'Absolutely unnecessary' which could give an (apparently) new programmer the idea that they are bad.
(sorry if I'm a bit grumpy today)
-
Oct 20th, 2008, 08:03 AM
#11
Re: [RESOLVED] Replacing a word in a sentence
Alright, no problem. I will repeat myself :
Using prefixes in variable declarations is "Absolutely unnecessary technique, however trying to give your variables some more meaningfull names is definitely a good idea".
For sample declaration without prefixes read my reply in post #4.
By all mean I'm not saying that using prefixes is a "bad idea" - only unnecessary.
In post VB6 era programmers are discurrraged from using them so it might be a better habbit not to use prefixes to begin with.
Best regards.
Last edited by RhinoBull; Oct 20th, 2008 at 08:06 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|