从字符串中查找字符组
Find Group of Characters From String
我做了一个程序来从字符串中删除一组字符。我在下面给出了这个编码。
void removeCharFromString(string &str,const string &rStr)
{
std::size_t found = str.find_first_of(rStr);
while (found!=std::string::npos)
{
str[found]=' ';
found=str.find_first_of(rStr,found+1);
}
str=trim(str);
}
std::string str ("scott<=tiger");
removeCharFromString(str,"<=");
至于我的程序,我正确得到了我的输出。 好的,好的。如果我给str一个值为"scott=tiger",那么在变量str中找不到可搜索字符"<="。但是我的程序也从值"scott=tiger"中删除了"="字符。但我不想单独删除字符。我想删除字符,如果我只找到找到的字符组"<="。我该怎么做?
>该方法find_first_of
在输入中查找任何字符,在您的情况下,是"<"或"="中的任何字符。在您的情况下,您想使用查找。
std::size_t found = str.find(rStr);
这个答案的工作原理是假设你只想按确切的顺序找到一组字符,例如,如果你想删除<=
但不删除=<
:
find_first_of
将找到给定字符串中的任何字符,您要在其中查找整个字符串。
你需要一些东西来达到以下效果:
std::size_t found = str.find(rStr);
while (found!=std::string::npos)
{
str.replace(found, rStr.length(), " ");
found=str.find(rStr,found+1);
}
str[found]=' ';
的问题在于它只会替换您正在搜索的字符串的第一个字符,因此如果您使用它,您的结果将是
scott =tiger
而通过我给你的改变,你会得到
scott tiger
相关文章:
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 查找字符在两个索引之间出现的次数
- 使用连续字符及其路径查找矩阵中最长路径的长度
- 在字符串中查找一对字符
- 在字符数组 (C++/根) 中查找双精度值
- C++ char 类查找字符索引
- 拆分字符串输入并查找字符是否有效
- 查找和比较两个字符串中的字符
- 从输入 1 查找输入 2 中字符的位置
- 查找单个字符串中重复次数最多和次数第二多的字符
- 如何查找两个字符串 c++ 中常用字符的数量
- 视觉C++查找以 "ing" 结尾且少于或等于 10 个字符的单词
- 如何从链表指针字符中查找字符
- 查找字符串中存在的不同字符
- LPCWSTR 查找和替换为转义字符
- 在C 中查找非空格字符
- 使用哈希 C++ 查找具有 k 个不同/唯一字符的最长子字符串
- 查找字符数组中的数字总和
- 提升正则表达式 cpp 用于查找 %% 之间的字符串,输出不包括 % 字符本身
- 查找并列出字符串C++中的所有字符簇