473,386 Members | 1,721 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,386 software developers and data experts.

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::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
5 7288


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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Pikkel | last post by:
i'm looking for a way to replace special characters with characters without accents, cedilles, etc.
24
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
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...
6
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...
4
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...
21
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...
6
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...
1
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 "":...
6
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...
8
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...

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.