温馨提示×

c++中ignore特定字符串的方法

c++
小樊
81
2024-09-25 03:22:12
栏目: 编程语言

在C++中,如果你想忽略特定的字符串,可以使用std::string::find()函数来查找字符串中是否包含该特定字符串,然后使用std::string::erase()函数来删除该特定字符串。

例如,假设你有以下字符串:

std::string str = "Hello, world!";
std::string ignore = "world";

你可以使用以下代码来删除ignore字符串:

size_t pos = str.find(ignore);
if (pos != std::string::npos) {
    str.erase(pos, ignore.length());
}

在上面的代码中,str.find(ignore)函数会在str字符串中查找ignore字符串的位置,如果找到了,则返回该位置的索引值,否则返回std::string::npos

然后,str.erase()函数会从str字符串中删除ignore字符串,删除的起始位置为pos,删除的长度为ignore.length()

0