String Erase commands

I have gone over the commands for string.erase and searched for how to do all the things one can with that command but I recall, maybe a year ago or so, that there was some other variation on it that worked differently.

Currently I'm doing things such as:

1
2
3
4
5
part1.erase(0, part1.find(","));
// or
part1.erase(part1.find(","));
// or
part1.erase(part1.(","), string::npos));


But I'm finding that its not working as I thought.

I'm reading from a text file an converting it to a string to chop it up, and put various variables into an array.

Example:
 
std::string part1 = "hi, 9, 38287.393, 3:30, lol";


WITHOUT using integers (as definitive positions within the string erase command)...

What commands would I have to use with string.erase to erase the comma after the "9" and everything after it? So that the resulting string would simply become "9" or whatever was between the 1st and second comma.

What about to erase only everything before the "30" (from the 3:30) and then everything after the 30 to leave me with just an integer to atoi or atof? So that the resulting string would be whatever was after the ":" and before the following comma?

The issue is these variables I'm ripping from the string are of varying length so using definitive numerical positions has not been successful. I've got so many manipulations I wrote into my code of this, as I thought I knew how to use it, but it appears its not working out well. Seems the first few conversions work, but after that it appears the commands are erasing the strings so I just need to avail myself of refreshing my mind.

This sites' string erase page doesn't show all the variational commands on string.erase to the extent I need them/understand them.
closed account (Dy7SLyTq)
part1.erase(part1.find(",")-1, part1.find(","));
i havent used erase much so i dont remember where it started erasing. this assumes it erases everything after and before the ints
As I read that one (if I understand it correctly), it seems it would change
 
part1 = "hi, 9, 38287.393, 3:30, lol";

into
 
part1 = "hi 38287.393, 3:30, lol";
Last edited on
closed account (Dy7SLyTq)
yeah so just put it in a loop to get all of them
I think what I'm missing in my understanding is I want to issue a command such as

"Find the first "," in the string, then erase everything before it".

AND

"Find the first "," and erase EVERYTHING after it"

So, erasing the first comma and everything before it would remove the "hi," and the second command would erase the comma after the 9 and everything after it, leaving just the "9"

so I can get

1
2
3
4
5
6
7
part1 = "9";
[code]

or

[code]
part1 = "30";


I don't see how to do that with your example
Last edited on
I think you are stating your problem incorrectly, because your statement implies you would erase everything in the string except for the first ','. You seem to want something like "the number/string in between the first two ',' characters", which is more complex. Is my understanding correct?
firedraco, to respond to your first statement, perhaps I am stating it incorrectly. I will try to explain it in a broader manner.

To answer your final question, yes, I want the characters/numbers BETWEEN certain symbols (typically colons, spaces, and or commas).

In the given string, I want to be able to take ANY part of it and be able to take that part of the string and erase everything else so all I have is that part of the string,

 
"hi, 9, 38287.393, 3:30, lol"


I want to know how to make the string = "hi", "9", or "38287", "393", "38287.393", "3", "30", or "lol".

I think if I knew how to issue the command "Find symbol X and erase everything before it" and command "Find symbol Y (or X) and erase everything after it" I could accomplish this. I've done it before, but I've lost the code and can't reference it. The problem is, that the various parts of the string may vary in how many spaces they take up, so I can't use definitive numerical values to represent the locations, I need to base it upon certain characters, such as ":", " ", "\n", ",", (colon, space, newline, or comma, respectively).

I previously thought it was something along the lines of:

1
2
3
4
if (part1.find(",")!=string::npos)
{
     part1.erase(",");
}


I thought that would erase everything up to and (possibly including) the "," but I don't see that happening. Or doing:

1
2
3
4
if (part1.find(","!=string::npos)
{
     part1.erase();
}


Thinking the "if" statement would move the position to the position of the comma and that the part1.erase() would take that position and erase everything after it, but my results, comparisons to what I've read on various websites (including this one) don't seem to be supportive of that.
Last edited on
Thank you for your input guys, I finally got over my frustration block and tested some additional variations on my question to get an answer.

1
2
3
4
5
if (part1.find("X")!=string::npos)
{
     part1.erase(part1.find("X"));
}


Erases "X" and everything after.

1
2
3
4
if (part1.find("X")!=string::npos)
{
     part1.erase(0, part1.find("X")+1);
}


Erases everything up to and including "X".

So I have my answer.

And, it would seem, usage of the IF statements will prevent my application from crashing when the string doesn't contain the necessary character to erase (big impediment to me figuring it out; the errors just added more frustration).
Last edited on
std::string::erase has the following form:

string& erase (size_t pos = 0, size_t len = npos);

There is no "position" stored in the string; you have to store that yourself.
I was thinking that the "npos" caused by the if statement was...i guess the term was "inherited" to the string.erase, but apparently not, but it works as a good check it would seem.
Topic archived. No new replies allowed.