Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Replacing parts of a string

I want to replace all -99999 in a line with just spaces.
I've got it working with the following code...but is there a more elegant way of doing it?


string entireLine = name+strID+strCP+strX1+strX2+strX3+strCD+strPS+strSEID;
string::size_type idx = 0;
while (true)
{
idx = entireLine.find("-99999", idx);
if (idx == string::npos)
break;
entireLine.replace(idx, 6," ");
idx += 6;
}
[479 byte] By [rssmps] at [2007-11-11 7:37:19]
# 1 Re: Replacing parts of a string
I think this code gets the job done faily efficiently and in in a non-terribly versbose manner. You could use a few advanced features such as predicated and transform() but I don't think they're really worth the effort in this case. One thing I might do differently: I would first scan the string for all positions of 999999, store the positions in a vector or something and then apply replace in a sequenc, using the previously stored indexes but that's up to you.
Danny at 2007-11-11 21:02:05 >