|
-
Dec 22nd, 2010, 11:17 AM
#1
Thread Starter
Fanatic Member
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

-
Dec 22nd, 2010, 12:15 PM
#2
Re: String Replace
 Originally Posted by vijy
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)
-
Dec 23rd, 2010, 12:30 AM
#3
Thread Starter
Fanatic Member
Re: String Replace
 Originally Posted by Edgemeal
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

-
Dec 23rd, 2010, 04:53 AM
#4
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|