Results 1 to 4 of 4

Thread: String Replace

  1. #1

    Thread Starter
    Fanatic Member vijy's Avatar
    Join Date
    May 2007
    Location
    India
    Posts
    548

    String Replace

    hi,
    i need to replace the whole word from a string.

    for example.
    String= "Hi VBFriends, I am learning VB 2010."

    i need to replace only VB as C#.

    if i put String.Replace, its changing as "Hi C#Friends,I am learning C# 2010."

    i used regex, Word boundary but not worked, below my code.

    Code:
    Dim sHTMLStream As String = "Hi VBFriends, I am learning VB 2010."
                Dim oColl As MatchCollection = Regex.Matches("sHTMLStream", "\\bvb\b", RegexOptions.IgnoreCase)
            For Each sTemp As Match In oColl
                MessageBox.Show(sTemp.Value)
            Next
    Visual Studio.net 2010
    If this post is useful, rate it


  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: String Replace

    Quote Originally Posted by vijy View Post
    hi,
    i need to replace the whole word from a string.

    for example.
    String= "Hi VBFriends, I am learning VB 2010."

    i need to replace only VB as C#.
    Couldn't you do it like this?...

    Code:
    Dim sHTMLStream As String = "Hi VBFriends, I am learning VB 2010."
    sHTMLStream = Regex.Replace(sHTMLStream, " vb ", " C# ", RegexOptions.IgnoreCase)
    MessageBox.Show(sHTMLStream)

  3. #3

    Thread Starter
    Fanatic Member vijy's Avatar
    Join Date
    May 2007
    Location
    India
    Posts
    548

    Re: String Replace

    Quote Originally Posted by Edgemeal View Post
    Couldn't you do it like this?...
    Code:
    Dim sHTMLStream As String = "Hi VBFriends, I am learning VB 2010."
    sHTMLStream = Regex.Replace(sHTMLStream, " vb ", " C# ", RegexOptions.IgnoreCase)
    MessageBox.Show(sHTMLStream)
    Yes, i tried but after vb if there is "\n" or "\r" in stream means,the word is not replacing.

    sHtmlStream i will take from a text file,

    "Hi VBFriends,
    I am learning VB
    2010"

    in the above case,the string wont replace.
    Visual Studio.net 2010
    If this post is useful, rate it


  4. #4
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: String Replace

    Try this

    Code:
    Regex.Replace(sHTMLstream, "\bvb\b", "C#", RegexOptions.IgnoreCase)
    I just tested it and it worked aok.

    The reason yours didn't work was because of your pattern.

    Code:
    pattern = "\\bvb\b"
    you put two slashes before the first wordboundary, a double slash is when you wish to include a slash literal (i.e your match will contain a "\" so you don't want the metacharacter) In effect, the pattern YOUR RegEx was looking for was the literal: "\bvb", which isn't what you want.
    Last edited by J-Deezy; Dec 23rd, 2010 at 05:03 AM.
    If I helped you out, please take the time to rate me

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