By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

replace special characters in string

P: n/a
Hi all,

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

for(string::size_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
Share this Question
Share on Google+
5 Replies


P: n/a


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::size_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

P: n/a
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::size_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::size_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

P: n/a
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

P: n/a
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::size_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::size_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

P: n/a


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.replace 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 discussion thread is closed

Replies have been disabled for this discussion.