请告诉我是否正确理解 substr 成员函数?
result = result.substr(0, pos) + result.substr(pos + 1);
它从 pos 获取字符串,0 直到(但不包括), remove[i]
然后+ result.substr(pos + 1);
连接字符串的其余部分,除了但不包括字符串 / char in remove
?
string removeLetters2(string text, string remove)
{
int pos;
string result = text;
for (int i = 0; i < remove.length(); i++)
{
while (true)
{
pos = result.find(remove[i]);
if (pos == string::npos)
{
break;
}
else
{
result = result.substr(0, pos) +
result.substr(pos + 1);
}
}
}
return result;
}