删除两个特定字符
Deleting two specific Characters
所以我有一个小问题,我想在C 中实现这一目标,但我不知道该怎么做:
给定的是一个包含随机数,符号和字母的字符串:
std::string = "1653gbdtsr362g2v3f3t52bv^hdtvsbjj;hdfuue,9^1dkkns";
现在我正在尝试找到所有^
字符,如果这些字符的数字在0到9之间,则删除^
和数字,因此:
"^1ghhu^7dndn^g"
变为:
"ghhudndn^g"
我知道如何从字符串中找到和替换/删除字符,但是我不知道如何以不硬编码的方式检查它是否遵循数字。任何帮助都将不胜感激。
std::string s = "^1ghhu^7dndn^g";
for (int i = 0; i < s.length() - 1; ++i)
{
if (s[i] == '^' && std::isdigit(s[i + 1]))
{
s.erase(i, 2);
--i;
}
}
这需要以下包括:
#include <string>
#include <cctype>
我会这样做:
#include <iostream>
#include <string>
#include <utility>
#include <iterator>
template<class Iter, class OutIter>
OutIter remove_escaped_numbers(Iter first, Iter last, OutIter out) {
for ( ; first != last ; )
{
auto c = *first++;
if (c == '^' && first != last)
{
c = *first++;
if (std::isdigit(c))
continue;
else {
*out++ = '^';
*out++ = c;
}
}
else {
*out++ = c;
}
}
return out;
}
int main()
{
using namespace std::literals;
auto input = "^1ghhu^7dndn^g"s;
auto output = std::string{};
remove_escaped_numbers(input.begin(), input.end(), std::back_inserter(output));
std::cout << output << std::endl;
}
或这样:
#include <iostream>
#include <regex>
int main()
{
using namespace std::literals;
auto input = "^1ghhu^7dndn^g"s;
static const auto repl = std::regex { R"___(^d)___" };
auto output = std::regex_replace(input, repl, "");
std::cout << output << std::endl;
}
使用std :: stringstream的解决方案,然后返回caret-digit的输入字符串。
#include <iostream>
#include <sstream>
#include <cctype>
int t404()
{
std::stringstream ss;
std::string inStr("1653gbdtsr362g2v3f3t52bv^hdtvsbjj;hdfuue,9^1dkkns");
for (size_t i = 0; i<inStr.size(); ++i)
{
if(('^' == inStr[i]) && isdigit(inStr[i+1]))
{
i += 1; // skip over caret followed by single digit
}
else
{
ss << inStr[i];
}
}
std::cout << inStr << std::endl; // compare input
std::cout << ss.str() << std::endl; // to results
return 0;
}
输出:
1653GBDTSR362G2V3F3T52BV^hdtvsbjj; hdfuue,9^1dkkns 1653GBDTSR362G2V3F3T52BV^HDTVSBJJ; HDFUUE,9DKKNS
您可以简单地绕字符串循环并在跳过不希望的字符时复制它。这是这样做的可能功能:
std::string filterString (std::string& s) {
std::string result = "";
std::string::iterator it = s.begin();
char c;
while (it != s.end()) {
if (*it == '^' && it != s.end() && (it + 1) != s.end()) {
c = *(it + 1);
if(c >= '0' && c <= '9') {
it += 2;
continue;
}
}
result.push_back(*it);
++ it;
}
return result;
}
强大的解决方案是使用c 11带来的正则库。
std::string input ("1653gbdtsr362g2v3f3t52bv^hdtvsbjj;hdfuue,9^1dkkns");
std::regex rx ("[\^][\d]{1}"); // "[^][d]{1}"
std::cout << std::regex_replace(input,rx,"woot");
>> 1653gbdtsr362g2v3f3t52bv^hdtvsbjj;hdfuue,9wootdkkns
这将找到一个"^"字符([^]
),然后是1({1}
)数字([d]
),并用"woot"
替换所有事件。
希望此代码可以解决您的问题:
#include <iostream>
#include <string>
int main()
{
std::string str = "^1ghhu^7dndn^g";
std::string::iterator first, last;
for ( std::string::iterator it=str.begin(); it!=str.end(); ++it)
{
if(*it == '^')
{
first = it;
it++;
while(isdigit(*it))
{
it++;
}
last = it - 1;
if(first != last)
{
if((last + 1) != str.end())
{
str.erase(first, last + 1);
}
else
{
str.erase(first, str.end());
break;
}
}
}
}
std::cout<< str << std::endl;
return 0;
}
输出:
$ ./erase
ghhudndn^g
相关文章:
- 查找字符在两个索引之间出现的次数
- 如何读取单个字符并在输入两个字符序列时输出? 使用 while 循环和C++
- 我已经建立了递归关系,它找到了两个字符串之间最长的连续公共字符串,我怎么能跳过其中一个字符串中的一个字符
- 函数返回两个字符串的第一个常用字符
- 在函数中连接两个字符,并在 C++ 中返回输出
- 为什么要更改这两个代码的输出"&"字符?
- 舍入 QDecDouble 值,精度最多为两个字符
- 计算两个字符序列
- 在c++中连接两个字符
- 查找和比较两个字符串中的字符
- 为什么C++中不允许连接两个常量字符*?
- 如何使用排序和比较这两个函数在 c++ 中对字符数组进行排序?
- 有没有办法我可以从 istringstream 中读取两个字符
- 如何查找两个字符串 c++ 中常用字符的数量
- 如何查看两个字符变量是否相等
- 使用位于 C 字符串中的两个字符*获取子字符串
- C++ std::string 中是否有任何函数可以计算两个字符串的相同起始字符的总数或任何最佳方法
- 重命名两个目录中的文件名,如果它们之间的某些字符匹配 - 矢量下标超出范围
- 比较两个字符的问题
- 如何修复密码条目,它一次输入两个字符