473,783 Members | 2,545 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

replace special characters in string

Hi all,

i want to replace $ to \$ so linux can work with paths and filenames
that contain $.
I wrote the following code

for(string::siz e_type i = s.find(exist, 0); i != string::npos; i =
s.find(foo, i))
{
s.replace(i, foo.size(), bar);
}

It's logical that this creates a loop that never ends, because i don't
replace the $, so my question is how to replace $ with \$ and then
jump to the next, instead of replacing \$ again..

tia...
Jul 22 '05 #1
5 7314


mr h q wrote:

Hi all,

i want to replace $ to \$ so linux can work with paths and filenames
that contain $.
I wrote the following code

for(string::siz e_type i = s.find(exist, 0); i != string::npos; i =
s.find(foo, i))
{
s.replace(i, foo.size(), bar);
}

It's logical that this creates a loop that never ends, because i don't
replace the $, so my question is how to replace $ with \$ and then
jump to the next, instead of replacing \$ again..


Well. If the $ happend to be at eg position 5 and you replace
$ with \$, then you know that the $ will move to position 6. Thus
if you continue searching at position 7, you will not interfere
with the text you replaced :-)

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #2
mr h q wrote:
Hi all,

i want to replace $ to \$ so linux can work with paths and filenames
that contain $.
I wrote the following code

for(string::siz e_type i = s.find(exist, 0); i != string::npos; i =
s.find(foo, i))
{
s.replace(i, foo.size(), bar);
}

It's logical that this creates a loop that never ends, because i don't
replace the $, so my question is how to replace $ with \$ and then
jump to the next, instead of replacing \$ again..


You have to start your following find() calls _after_ the replaced
string, not at the same location again. So try something like:

for(string::siz e_type i = s.find(exist, 0); i != string::npos;
i = s.find(foo, i + bar.size()))
{
s.replace(i, foo.size(), bar);
}

This should be save, since after the replacement, the string will be at
least i + bar.size() long.

Jul 22 '05 #3
On Mon, 24 Nov 2003 15:53:52 +0100, Karl Heinz Buchegger wrote:

mr h q wrote:

Hi all,

i want to replace $ to \$ so linux can work with paths and filenames
Well. If the $ happend to be at eg position 5 and you replace
$ with \$, then you know that the $ will move to position 6. Thus
if you continue searching at position 7, you will not interfere
with the text you replaced :-)


In this case wouldn't the character that was originally at position 6 be
overwritten with $? I think he also needs to be careful to copy everything
after the $ to one position later in the string.
Jul 22 '05 #4
Rolf Magnus <ra******@t-online.de> wrote in message news:<bp******* ******@news.t-online.com>...
mr h q wrote:
Hi all,

i want to replace $ to \$ so linux can work with paths and filenames
that contain $.
I wrote the following code

for(string::siz e_type i = s.find(exist, 0); i != string::npos; i =
s.find(foo, i))
{
s.replace(i, foo.size(), bar);
}

It's logical that this creates a loop that never ends, because i don't
replace the $, so my question is how to replace $ with \$ and then
jump to the next, instead of replacing \$ again..


You have to start your following find() calls _after_ the replaced
string, not at the same location again. So try something like:

for(string::siz e_type i = s.find(exist, 0); i != string::npos;
i = s.find(foo, i + bar.size()))
{
s.replace(i, foo.size(), bar);
}

This should be save, since after the replacement, the string will be at
least i + bar.size() long.

It works now...thanks!!
Jul 22 '05 #5


Brad Marts wrote:

On Mon, 24 Nov 2003 15:53:52 +0100, Karl Heinz Buchegger wrote:
mr h q wrote:

Hi all,

i want to replace $ to \$ so linux can work with paths and filenames
Well. If the $ happend to be at eg position 5 and you replace
$ with \$, then you know that the $ will move to position 6. Thus
if you continue searching at position 7, you will not interfere
with the text you replaced :-)


In this case wouldn't the character that was originally at position 6 be
overwritten with $?


No, the test is *inserted*, meaning all text right to that position
gets shifted to the right.

If you replace $ with \$ in
abcd$efg

you get
abcd\$efg

The string got longer!
I think he also needs to be careful to copy everything
after the $ to one position later in the string.


std::string.rep lace does that already.
The OP's question turns around the find function used to find the next occurence
of the text to replace:

abc$def$ghi

find finds the first $ at position 3. Replacing that with the replacement text
leads to

abc\$def$ghi

Now if the find is restarted at position 4, it immediatly will find another $
at position 4. Doing the replacement leads to

abc\\$def$ghi

and so on and so on. The solution of course is to start the next find at the
end of the replaced text.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #6

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

Similar topics

17
47821
by: Pikkel | last post by:
i'm looking for a way to replace special characters with characters without accents, cedilles, etc.
24
4510
by: Wim Roffal | last post by:
Is there a possibility to do a string replace in javascript without regular experessions. It feels like using a hammer to crash an egg. Wim
14
3279
by: Etu | last post by:
Hi, I have a string: string c = "'abc' \"cde\", 'mno' \"xyz\","; how can I use the c.Replace(???, ???) method to have this string: "'abc' "cde", 'mno' "xyz"," that is, all the backslashes are removed.
6
8885
by: Chris Anderson | last post by:
Anyone know of a fix (ideally) or an easy workaround to the problem of escape characters not working in regex replacement text? They just come out as literal text For example, you'd think that thi Regex.Replace("<stuff>text</stuff>", "<stuff>", "<stuff>\n" would give yo <stuff text</stuff
4
4369
by: jgabbai | last post by:
Hi, What is the best way to white list a set of allowable characters using regex or replace? I understand it is safer to whitelist than to blacklist, but am not sure how to go about it. Many thanks!
21
3412
by: gary | last post by:
How would one make the ECMA-262 String.replace method work with a string literal? For example, if my string was "HELLO" how would I make it work in this instance. Please note my square brackets are not regular expression syntax. Thanks,
6
6805
by: Gabriel | last post by:
Hello, I do this : s = Environment.CurrentDirectory; s = s.Replace("\\", @"\"); Environment.CurrentDirectiry return a path like this C:\\....\\....\\..... I'd like replace the \\ by \, but the code I use not work. Any idea ? Best Regards,
1
3408
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "": --------------------------------- myStringVar = myStringVar.replace("^" + iName + "=,($|&)", ""); --------------------------------- The following DOES replace it though: --------------------------------- var match = myStringVar.match("^" + iName + "=,($|&)");
6
3096
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello, I have some XML that is returned to my application from another vendor that I cannot change before it gets to me. I can only alter it after it gets to my application. That being said, I am having a problem loading the XML correctly into my app. Here is the code: ==================================== Dim sPaymentXML as String
8
4656
by: Kurt Peters | last post by:
I'm using the code below to read a pdf document, and it has no line feeds or carriage returns in the imported text. I'm therefore trying to just replace the symbol that looks like it would be an end of line (found by examining the characters in the "for loop") unichr(167). Unfortunately, the replace isn't working, does anyone know what I'm doing wrong? I tried a number of things so I left comments in place as a subset of the bunch of...
0
9643
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
10147
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
10083
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
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
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
7494
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
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.