Results 1 to 11 of 11

Thread: [RESOLVED] Replacing a word in a sentence

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2008
    Posts
    38

    Resolved [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.

  2. #2

  3. #3
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: Replacing a word in a sentence

    Quote 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.

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Replacing a word in a sentence

    Quote 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.

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2008
    Posts
    38

    Re: Replacing a word in a sentence

    Quote 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.

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Replacing a word in a sentence

    Quote 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:
    Code:
    VBA.Replace(....)
    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.

  7. #7
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    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?

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: [RESOLVED] Replacing a word in a sentence

    No, I'm just saying that Replace is not a reserved word

  9. #9

  10. #10
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    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)

  11. #11
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width