Results 1 to 4 of 4

Thread: [2005] What's the best way to change a value in a string?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    211

    [2005] What's the best way to change a value in a string?

    I have a string:
    Code:
    myString = "A=Bob, B=Cat, C=Tomato, and the crowd goes wild"
    I want to always change C to C=Potato. No matter what already is there (wont always be Tomato).

    In VB6 I did this:

    Code:
    start = InStr(1, UCase(myString), UCase("C=")) + Len("C=")
    pos_end = InStr(start + 1, myString, ",")
    NewString = Mid(myString, 1, start) & "Potato" & Mid(myString, pos_end)
    Which worked fine, but I wondered if there was a better way for VB2005
    Last edited by DssTrainer; Jun 21st, 2007 at 03:15 PM.

  2. #2
    Hyperactive Member ProphetBeal's Avatar
    Join Date
    Aug 2006
    Location
    New York
    Posts
    424

    Re: [2005] What's the best way to change a value in a string?

    Here is 1 way you can do it in VB2005, but I'm sure there are others
    VB.NET Code:
    1. Dim myString As String = "A=Bob, B=Cat, C=Tomato, and the crowd goes wild"
    2. Dim strResult As String = ""
    3. Dim intStart As Integer = myString.IndexOf("C=")
    4. If intStart > 0 Then
    5.     strResult = myString.Substring(0, intStart + 2) & "Potato" & myString.Substring(myString.IndexOf(",", intStart))
    6. End If
    7. MessageBox.Show(strResult)

    Helpful Links:
    VB 6 - How to get the "Key" Value in a collection
    VB.NET - File Search Utility || VB.NET - How to compare 2 directories || VB.NET - How to trust a network share
    VB.NET - Create Excel Spreadsheet From Array || VB.NET - Example Code & Hints you may not know
    VB.NET - Save JPEG with a certain quality (image compression) || VB.NET - DragDrop Files, Emails, and Email Attachments

    Please post some of the code you need help with (it makes it easier to help you)
    If your problem has been solved then please mark the thread [RESOLVED].
    Don't forget to Rate this post

    "Pinky, you give a whole new meaning to the phrase, 'counter-intelligence'."-The Brain-

  3. #3
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] What's the best way to change a value in a string?

    If you are going to use a list with keys like this take a look at the Dictionary Generic Class.

    Hope it helps.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] What's the best way to change a value in a string?

    If you have a specific pattern of characters you want replaced you can also use Regex.Replace. If you know for a fact that you want to replace "C=somewordhere" then a Regex is probably the way to go.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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