Yoda
Yoda

Reputation: 18068

trimming a string in c++

Hello I have a problem with trimming string in c++. It adds some weird chars at the end of the string.

string& Napis::subst(char cold, char cnew) { 
    string * s = new string(data); 
    replace(s->begin(),s->end(), cold, cnew);  
    return *s; // takes back string s with changed sign
}

Upvotes: 0

Views: 185

Answers (1)

David Schwartz
David Schwartz

Reputation: 182753

I'll bet you are calling subst with cnew set to zero. You are expecting this to delete the characters, but that's not what it does. It replaces them with zeroes, just as its name suggests.

How about:

string Napis::subst(char cold, char cnew) { 
    assert(cnew != 0);
    string s = data; 
    replace(s.begin(), s.end(), cold, cnew);  
    return s; // takes back string s with changed sign
}

Upvotes: 1

Related Questions