views:

63

answers:

1

Hello,

string DelStr = "I! am! bored!";
string RepStr = "10/07/10"

I want to delete all '!' on DelStr and I want to replace all '/' with '-' on the RepStr string.

Is there any way to do this without doing a loop to go through each character?

A: 

Remove the exclamations:

#include <algorithm>

std::string result;
result.resize(delStr.size());
std::remove_copy(delStr.begin(), delStr.end(), result.begin(), '!');

Alternatively, if you want to print the string, you don't need the result variable:

#include <iterator>

std::remove_copy(delStr.begin(), delStr.end(),
                 std::ostream_iterator<char>(std::cout), '!');

Replace slashes with dashes:

std::replace(repStr.begin(), repStr.end(), '/', '-');
larsmans
Thanks! Works great.
Cornwell