473,698 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(myT ext, "hallo",
RegexOptions.Mu ltiline Or RegexOptions.Ig noreCase)

Do While m.Success
myText = Replace(myText, m.Value, m.Value & " world")
m = m.NextMatch
Loop


Sep 26 '06 #1
3 1491
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(myT ext, "hallo",
RegexOptions.Mu ltiline Or RegexOptions.Ig noreCase)

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.T ext)

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.T ext)

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
1653
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...) Windows 2000 Small Business Server Using regular expressions for the first time, and having difficulty with the replace method. We're searching through a list of vehicle models, trying to replace what we get from the client data with correctly...
2
2425
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 code below is the basic way to use the replace method but it only allows for one textbox. I'm sure I need a loop in there but I'm not sure how to use that
4
3824
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" or "newline<p>" combo? Thanks for any advice!
23
6987
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 return/line feed" or "line feed/carriage return" and removing them. I quess what I need is an editor that allows non-printable characters in it's search strings. Does anyone know of on that will allow this? Thank you...
3
1448
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 assume that I'm not familiar w/ the structure of the XML so that I'll have to read/replace/load via stream. Is this possible OpenStream->Replace char(s)->To XML parser ? If so then how? Art
8
5922
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 may be lots and lots of these entries (they're pulled from my mythtv database). I could brute-force this by rewriting the whole thing and replacing
4
3025
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 the following snippet in the online help but the output does not change at all:
7
3789
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. The list also needs to be editable by the end-user so I'll be providing a form on which they can edit the allowed character list. The end-user is non-technical so asking them to type a regular expression is out.
1
3627
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 that means that the unicode number in base 10 is 194. So I want to do a String.Replace(mystr,194,''c) But how do I convert the 194 into a Char, and how do I replace that by blanks. Thanks, Marvin
0
1369
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
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9171
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8905
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.