C++ 从字符串中删除标点符号
C++ Remove punctuation from String
我得到了一个字符串,我想从中删除所有标点符号。我该怎么做?我做了一些研究,发现人们使用 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);
- 将数组的地址分配给变量并删除
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C/C++编译器通常会删除重复的库吗
- 从链接列表c++中删除一个项目
- C++如何通过用户输入删除列表元素
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 是否需要删除包含对象的"pair"?
- 在 c++ 中打开文件、删除标点符号并追加到另一个文件中
- 删除标点符号和空间的问题
- C 删除某些符号/标点符号
- 如何删除字符串中的标点符号
- 在 c++ 中删除标点符号和空格的最佳方法是什么
- C++ 从字符串中删除标点符号
- 如何删除句子中的所有标点符号
- 使用ispunct()删除标点符号
- 从字符串中删除标点符号
- 从字符数组中删除空白和标点符号的函数
- 编写一个程序,读取一串包含标点符号的字符,并写入被读取的内容,但删除了标点符号
- 删除字符串c++开头和结尾的标点符号
- C++删除字符串上的标点符号,擦除()/迭代器问题