• 0

C++ Removing Certain Characters From A String


Question

Well I'm a noobie so go easy on me. :p

Basically I'm making a simple hash converter that converts windows password hashes from the login recovery program to standard lm format or pwdump format....

So I've gotten the program to extract the lines of a hash from a text file specified by the user, but now I need to remove spaces, colons, etc from the string.

For example a hash looks like this:

Administrator:500,45,B2,60,80,90,34,C1,20,61,EF,18,F9,7A,FA,59,49,33: _25,26,AD,9B,F9,3F,A4,39,34,4C,2C,FD,9E,15,D0,DC,XX:::

So I want to remove the space between the 33: and the _

Then I want to remove the colons ":" and the commas...not specifically in that order but w/e.

Right now I'm using:

remove(line1.begin(), line1.end(), ' ');

remove(line1.begin(), line1.end(), '_');

remove(line1.begin(), line1.end(), ':');

remove(line1.begin(), line1.end(), ',');

But for some reason it adds extra characters on to the end of the string, for example instead of the end of the string being:

XX:::

It removes the colons and everything from the beginning of the string, but the end then looks like this:

[code]XXXX::::

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Thanks for the question, I had to scratch my head a bit to find the solution.

Basically, what remove does is shift the string left each time it finds a matching character, while keeping the string of the same length. For instance, say you have:

string str = "abcc";
remove(str.begin(), str.end(), 'b');
cout << str << endl;

The ouput is :

accc

remove returns an iterator to the new "end" of the string, so, use that to trim the string to its new length.

string::iterator newEnd = remove(str.begin(), str.end(), character);
str.erase(newEnd, str.end());

http://www.cplusplus.com/reference/algorithm/remove.html

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.