C++ 从字符串中删除标点符号 [英] C++ Remove punctuation from String

查看:68
本文介绍了C++ 从字符串中删除标点符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I got a string and I want to remove all the punctuations from it. How do I do that? I did some research and found that people use the ispunct() function (I tried that), but I cant seem to get it to work in my code. Anyone got any ideas?

#include <string>

int main() {

string text = "this. is my string. it's here."

if (ispunct(text))
text.erase();

return 0;
}

推荐答案

我明白了.

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

这篇关于C++ 从字符串中删除标点符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆