473,394 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

REPLACE method: unwanted multiple-replacement

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


Sep 26 '06 #1
3 1467
teo wrote:
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

Sep 26 '06 #2
teo wrote:
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.
Sep 26 '06 #3
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!

Sep 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: William Morris | last post by:
Been pounding on this for an hour and eating up my day's productivity, so I thought I'd throw a question out there. (I'm also reading through the Google archives to try to find this answer...) ...
2
by: Barnes | last post by:
Hi, Can anyone please tell me how I can use the replace method to replace a character if it occures in more than one textbox without having to write separate function for each textbox. The...
4
by: Robert Mark Bram | last post by:
Hi All, I have the following to replace newline chars with <br> in a string: ..replace(/\n/g,"<br>") How can I change this so that it replaces only if there is not already a "<br>newline"...
23
by: SeaPlusPlus | last post by:
I want to convert large files of prose to xhtml and so I need a way to remove unwanted line wraps. So, I'm looking for a freebee editor that has the capability of searching for a single "carriage...
3
by: Art | last post by:
What's the most efficient way to replace characters in an XML document before it is loaded into a parser? Chars I'd want to replace are in attributes and there can be N attributes, also let's...
8
by: Captain Dondo | last post by:
I have an array(?) (sorry, I'm new* to python so I'm probably mangling the terminology) that looks like this: I want to replace every instance of 'tooth.seiner.lan' with 'localhost'. There...
4
by: vvenk | last post by:
Hello: I have a string, "Testing_!@#$%^&*()". It may have single and double quotations as well. I would like to strip all chararcters others than a-z, A-Z, 0-9 and the comma. I came across...
7
by: Grok | last post by:
I need an elegant way to remove any characters in a string if they are not in an allowed char list. The part cleaning files of the non-allowed characters will run as a service, so no forms here. ...
1
by: COHENMARVIN | last post by:
I have a string with an unwanted unicode character. It looks like an 'A' with a tilde on top. I looked up a unicode chart on the internet and the chart says that its represented by  So I think...
0
by: Alexey Smirnov | last post by:
On Jul 10, 11:03 pm, Alexey Smirnov <alexey.smir...@gmail.comwrote: Sorry, the one with a tilde on top is Â
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.