Home All Groups Group Topic Archive Search About

REPLACE method: unwanted multiple-replacement

Author
21 Sep 2006 8:39 PM
teo
I have a text.

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

Author
26 Sep 2006 8:00 PM
teslar91
teo wrote:
Show quoteHide quote
> I have a text.
>
> 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?

Hi Teo,

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
Author
26 Sep 2006 8:05 PM
ShaneO
teo wrote:
Show quoteHide quote
> I have a text.
>
> 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
>
You could always just do -

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.
Author
26 Sep 2006 8:55 PM
teslar91
ShaneO wrote:
> You could always just do -
>
> 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")

Heh, much nicer.  Shows how much I still have to learn about VB.NET -
this is day #3 for me.  Thanks!