C++ 从字符串中删除标点符号

C++ Remove punctuation from String

本文关键字:删除 标点符号 字符串 C++      更新时间:2023-10-16

我得到了一个字符串,我想从中删除所有标点符号。我该怎么做?我做了一些研究,发现人们使用 ispunct(( 函数(我试过了(,但我似乎无法让它在我的代码中工作。有人有什么想法吗?

#include <string>
int main() {
string text = "this. is my string. it's here."
if (ispunct(text))
text.erase();
return 0;
}

使用算法remove_copy_if :-

string text,result;
std::remove_copy_if(text.begin(), text.end(),            
                        std::back_inserter(result), //Store output           
                        std::ptr_fun<int, int>(&std::ispunct)  
                       );

如果您需要将结果作为新字符串,POW 已经有一个很好的答案。这个答案是如果你想要就地更新,如何处理它。

食谱的第一部分是std::remove_if,它可以有效地去除标点符号,打包所有非标点符号。

std::remove_if (text.begin (), text.end (), ispunct)

不幸的是,std::remove_if不会将字符串缩小到新的大小。它不能,因为它无法访问容器本身。因此,在打包结果之后,字符串中会留下垃圾字符。

为了处理此问题,std::remove_if返回一个迭代器,该迭代器指示仍需要的字符串部分。这可以与字符串erase方法一起使用,导致以下成语...

text.erase (std::remove_if (text.begin (), text.end (), ispunct), text.end ());

我称之为成语,因为它是一种在许多情况下都有效的常用技术。除string以外的其他类型提供了合适的erase方法,std::remove(可能还有一些我暂时忘记的其他算法库函数(采用这种方法来缩小它们删除的项目的间隙,但将容器大小调整留给调用方。

#include <string>
#include <iostream>
#include <cctype>
int main() {
    std::string text = "this. is my string. it's here.";
    for (int i = 0, len = text.size(); i < len; i++)
    {
        if (ispunct(text[i]))
        {
            text.erase(i--, 1);
            len = text.size();
        }
    }
    std::cout << text;
    return 0;
}

输出

this is my string its here

删除字符时,字符串的大小会更改。每当发生删除时,都必须更新它。并且,您删除了当前字符,因此下一个字符将成为当前字符。如果不递减循环计数器,则不会检查标点字符旁边的字符。

ispunct采用char值而不是字符串。

你可以像

for (auto c : string)
     if (ispunct(c)) text.erase(text.find_first_of(c));

这将起作用,但这是一个缓慢的算法。

Steve314 的回答相当不错。我想添加一个小改动:

text.erase (std::remove_if (text.begin (), text.end (), ::ispunct), text.end ());

在函数 ispunct 之前添加 :: 负责重载。

这里的问题是 ispunct(( 将一个参数作为字符,而您正在尝试发送一个字符串。您应该遍历字符串的元素并擦除每个字符,如果它是标点符号,如下所示:

for(size_t i = 0; i<text.length(); ++i)
  if(ispunct(text[i]))
    text.erase(i--, 1);
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
    string str = "this. is my string. it's here.";
    transform(str.begin(), str.end(), str.begin(), [](char ch)
    {
        if( ispunct(ch) )
            return '';
        return ch;
    });
}
 #include <iostream>
 #include <string>
 using namespace std;
 int main()
 {
   string s;//string is defined here.
  cout << "Please enter a string with punctuation's: " << endl;//Asking for users input
  getline(cin, s);//reads in a single string one line at a time
/* ERROR Check: The loop didn't run at first because a semi-colon was placed at the end 
                of the statement.  Remember not to add it for loops. */
        for(auto &c : s)  //loop checks every character 
        {       
             if (ispunct(c)) //to see if its a punctuation
              {
               c=' ';       //if so it replaces it with a blank space.(delete) 
              }
        }
        cout <<  s << endl; 

   system("pause");
   return 0;
   }

另一种方法如下:

#include <ctype.h> //needed for ispunct()
string onlyLetters(string str){
    string retStr = "";
    for(int i = 0; i < str.length(); i++){
        if(!ispunct(str[i])){
            retStr += str[i];
        }
    }
    return retStr;

这最终会创建一个新字符串,而不是实际从旧字符串中删除字符,但与使用一些更复杂的内置函数相比,绕开你的头要容易一些。

我试图应用@Steve314的答案,但直到我在 cppreference.com 上遇到这个注释才让它工作:

笔记

<cctype>的所有其他函数一样,std::ispunct的行为 如果参数的值既不能表示为 unsigned char也不等于EOF。若要安全地使用这些函数,请使用 普通char s(或 signed char s(,参数应首先转换 到unsigned char .

通过研究它提供的示例,我能够使其像这样工作:

#include <string>
#include <iostream>
#include <cctype>
#include <algorithm>
int main()
{
    std::string text = "this. is my string. it's here.";
    std::string result;
    text.erase(std::remove_if(text.begin(),
                              text.end(),
                              [](unsigned char c) { return std::ispunct(c); }),
               text.end());
    std::cout << text << std::endl;
}

尝试使用这个,它将删除文本文件中字符串上的所有标点符号oky。

str.erase(remove_if(str.begin(), str.end(), ::ispunct), str.end());

如果有帮助,请回复

我明白了。

size_t found = text.find('.');
text.erase(found, 1);