Deleting characters from a string.

I want to remove '0' from a string but i don't know how to so. For example i have a string that says '0008' and i want to remove those '0' and just leave the '8'. I saw the command <name_of_string>.erase(); but it doesn't seem to work right. This is my function:

1
2
3
4
5
6
7
8
9
10
11
string fixResult()
{
    int sz;
    sz=result.size();
    for(int i=0;i<sz;i++)
    {
    if(result[i]=='0'&&result[i+1]!='0')
        {result=result.erase(0, i);}
    }
}
    return result;


It doesn't give me an error. The answer is just blank.
Last edited on
A few problems with your function:

1) Where is result defined? It should be passed in by reference. It should not be global.

2) Line 11 is out of place, it should be after line 9.

3) Line 6: You should be exiting the function when you find the first non-zero character.
1
2
  if (result[i] != '0')
    return result;


4) Line 8: You're erasing the 0'th character for i characters, but your loop is at the i'th character.
You should user iterators instead:
1
2
3
4
5
6
7
for(auto it = mystring.begin(); it != mystring.end(); ++it)
{
    while(it != mystring.end() && *it == '0')
    {
        it = mystring.erase(it);
    }
}
Last edited on
string::erase(pos,len) erases len characters starting at position pos. You are erasing i characters starting at the beginning.

Also, be careful with your for loop. If you erase a character, then next char in the string moves to the position previously occupied by the character erased. You need to check if THAT character needs to be erased, so you don't want to increment i in that case.

Since the size of the string changes through the loop, you can't pre-compute the size. You need to check against the current size each time.

Your if condition is a little odd too. Why do you care about the value of the next character? Isn't it sufficient to say that the current character is a '0'?

Finally, consider using a iterator instead and notice that erase(iterator) returns a new iterator. This is sort of overkill, but it will get you accustomed to the way you can remove items from an STL collection.
http://www.cplusplus.com/reference/string/basic_string/erase/

Thanks for the help. The mistakes i made are really stupid and one of them was because i copy/pasted the code wrong here. I changed the whole function. Now it works just fine.

1
2
3
4
5
6
7
8
9
10
11
12
string fixResult(string res)
{
    int b=0;
if(res[b]!='0') {return res;}
    while(res[b]=='0')
    {
    if(res[b]=='0'&&res[b+1]!='0')
        {res=res.erase(0,b+1);     }
        b++;
    }
   return res;
}


Sorry, but i have no idea what a "iterator" is. And dhayden i made a mistake by making a for loop instead of a while one. The if condition is to check if the next character is going to be '0'. If != '0' so it can delete all the characters from the beginning to now. I think if you look at the new function you will get the idea.
Last edited on
Your function now removes '0' characters at the beginning of the string, but not anywhere else. For example, it won't change "808." Is this what you intended? Your original post just said you wanted to remove '0' characters from a string.
Topic archived. No new replies allowed.