删除两个特定字符

Deleting two specific Characters

本文关键字:字符 两个 删除      更新时间:2023-10-16

所以我有一个小问题,我想在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