例如,我有一个用户输入一个电话号码。
cout << "Enter phone number: ";
INPUT: (555) 555-5555
cin >> phone;
我想从字符串中删除“(”,“”)和“-”字符。
发布于 2018-02-08 09:38:38
string str("(555) 555-5555");
char chars[] = "()-";
for (unsigned int i = 0; i < strlen(chars); ++i)
{
// you need include <algorithm> to use general algorithms like std::remove()
str.erase (std::remove(str.begin(), str.end(), chars[i]), str.end());
}
// output: 555 5555555
cout << str << endl;
功能:
void removeCharsFromString( string &str, char* charsToRemove ) {
for ( unsigned int i = 0; i < strlen(charsToRemove); ++i ) {
str.erase( remove(str.begin(), str.end(), charsToRemove[i]), str.end() );
}
}
//example of usage:
removeCharsFromString( str, "()-" );
https://stackoverflow.com/questions/-100003477
复制相似问题