replace special characters in string

M

mr h q

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...
 
K

Karl Heinz Buchegger

mr said:
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 :)
 
R

Rolf Magnus

mr said:
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.
 
B

Brad Marts

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.
 
M

mr h q

Rolf Magnus said:
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!!
 
K

Karl Heinz Buchegger

Brad said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top