|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
REPLACE method: unwanted multiple-replacementInside the text the "hallo" word occurs five time. I need to replace "hallo" with "hallo world". Unfortunately I get this: hallo world world world world world Obviously I only need: hallo world How can avoid this? ------ Here the simple Replace method code I'm currently using: Dim myText as String = "Hallo to everyone; I'd like to say hallo because saying hallo is a good thing; you also should say hallo. Hallo guy." Dim m As Match = Nothing m = Regex.Match(myText, "hallo", RegexOptions.Multiline Or RegexOptions.IgnoreCase) Do While m.Success myText = Replace(myText, m.Value, m.Value & " world") m = m.NextMatch Loop teo wrote:
Show quoteHide quote > I have a text. Hi Teo,> > Inside the text > the "hallo" word occurs five time. > > I need to replace "hallo" with "hallo world". > > Unfortunately I get this: > hallo world world world world world > > Obviously I only need: > hallo world > > How can avoid this? Is this what you want as a result? "hallo world hallo world hallo world hallo world hallo world" If so, here's a function from my personal library which will do that. Note that it's freshly converted from VB6, so it doesn't take advantage of any new language features and is going to be slow and inefficient with large strings: Function SReplace(ByVal X As String, ByVal Y As String, ByVal z As String) As String ' Replaces every occurence of substring Y in string X with Z. ' Avoids recursive replaces. Dim l1 As Long, l2 As Long, l3 As Long l2 = Len(Y) l3 = 1 Do l1 = InStr(l3, X, Y) : If l1 = 0 Then Exit Do X = Left$(X, l1 - 1) & z & Mid$(X, l1 + l2) l3 = l1 + Len(z) Loop SReplace = X End Function teo wrote:
Show quoteHide quote > I have a text. You could always just do -> > Inside the text > the "hallo" word occurs five time. > > I need to replace "hallo" with "hallo world". > > Unfortunately I get this: > hallo world world world world world > > Obviously I only need: > hallo world > > How can avoid this? > > ------ > > Here the simple Replace method code > I'm currently using: > > Dim myText as String = "Hallo to everyone; I'd like to say hallo > because saying hallo is a good thing; you also should > say hallo. Hallo guy." > > Dim m As Match = Nothing > m = Regex.Match(myText, "hallo", > RegexOptions.Multiline Or RegexOptions.IgnoreCase) > > Do While m.Success > myText = Replace(myText, m.Value, m.Value & " world") > m = m.NextMatch > Loop > myText = Strings.Replace(myText, "hallo", "hallo world", , , CompareMethod.Text) OR - if you need to retain case - myText = Strings.Replace(myText, "hallo", "hallo world") myText = Strings.Replace(myText, "Hallo", "Hallo world") ShaneO There are 10 kinds of people - Those who understand Binary and those who don't. ShaneO wrote:
> You could always just do - Heh, much nicer. Shows how much I still have to learn about VB.NET -> > myText = Strings.Replace(myText, "hallo", "hallo world", , , > CompareMethod.Text) > > OR - if you need to retain case - > > myText = Strings.Replace(myText, "hallo", "hallo world") > myText = Strings.Replace(myText, "Hallo", "Hallo world") this is day #3 for me. Thanks!
check username and password in database
A question of design Encrypt a date to use in demo version Detect right-click in MDI container's client area? System.Diagnostics.Process.Start Freezes No Recursive instr?!! RTf to Word.Doc Visual Studio Proffessional 2005 Finding embedded controls? C# to VB Conversion Help - Overriding Events? |
|||||||||||||||||||||||