Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Here's some code that removes the specified character, ch, from the string passed in. Is there a better way to do this? Specifically, one that's more efficient and/or portable?

//returns string without any 'ch' characters in it, if any.
#include <string>
using namespace std;
string strip(string str, const char ch)
{
        size_t p = 0; //position of any 'ch'

        while ((p = str.find(ch, p)) != string::npos)
                str.erase(p, 1);

        return str;
}
share|improve this question
1  
Where is iostream involved in this? Also, there's a one-liner version of it, though I'm not quite sure how the performance will compare: str.erase(std::remove(str.begin(), str.end(), ch), str.end()); –  Corbin Nov 10 '12 at 1:21
    
@Corbin: You could put that as an answer. –  Loki Astari Nov 10 '12 at 1:37
    
@Corbin, Not sure why I said iostream. Fixed. –  Nick Nov 10 '12 at 13:11

2 Answers 2

up vote 4 down vote accepted

I'm not entirely sure how the performance will compare, but the standard way to accomplish this would be the erase-remove idiom:

str.erase(std::remove(str.begin(). str.end(), ch), str.end());

Unless the performance proves to be a bottleneck, it's typically better to stick with the C++ style of doing things. I can't imagine that this would be significantly less efficient than the other method. (In fact, I wouldn't be surprised if this is a bit faster for long strings with a high amount of the removed character -- though my assumption of that depends on quite a few non-guaranteed implementation choices, and a rather rough estimation of the cost of different low level operations.)

share|improve this answer
    
Never heard of this idiom before now. Thanks. One question though: How is this C++ style? Because of the use of iterators rather than the loop? –  Nick Nov 10 '12 at 13:13
    
@Nick It's hard to explain. It's partly because of the iterators, but also partly because it uses the standard library instead of a custom loop. For the same reason that people avoid creating their own sorting methods, people try to use the standard library as much as possible (within reason of course). It's just more idiomatic to C++. (This was a pretty terrible explanation, but hopefully it made a little sense :).) –  Corbin Nov 10 '12 at 20:54
    
Nope, I got it. Thanks. I was actually hoping there was a standard library function for this, but didn't see one. But now I see that one of the other overloads is what I needed! –  Nick Nov 10 '12 at 21:06

My way (probably sloppy/inefficient ):

std::string StripCharacter( __in std::string StdString, __in const char Character )
{
    std::string Result = "";
    for( unsigned int Index = 0; Index != StdString.length( ); ++ Index )
        if( StdString[ Index ] != Character )
            Result += StdString[ Index ];
    return ( Result );
}

or...

std::string StripCharacter( __in std::string StdString,
                            __in const char Character,
                            __in unsigned int Start,
                            __in unsigned int End )
{
    if( Start >= End )
        return ( StdString );

    std::string Result = "";

    if( Start > 0 )
        Result += StdString.substr( 0, Start );

    for( unsigned int Index = Start; Index != End; ++ Index )
        if( StdString[ Index ] != Character )
            Result += StdString[ Index ];

    if( End < StdString.length( ) )
        Result += StdString.substr( End, StdString.length( ) - End );

    return ( Result );
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.