如何将C语言中的字符串去除所有标点符号?
- 内容介绍
- 文章标签
- 相关推荐
本文共计310个文字,预计阅读时间需要2分钟。
要将字符串中的所有标点符号删除,可以使用Python的字符串方法 `translate()` 结合 `str.maketrans()`。以下是一个简单的示例代码,它将删除字符串中的所有标点符号:
pythonimport string
def remove_punctuation(input_str): # 创建一个翻译表,将所有标点符号映射到None translator=str.maketrans('', '', string.punctuation) # 使用translate()方法删除标点符号 return input_str.translate(translator)
示例使用input_str=我有一些标点符号,比如!?,。result=remove_punctuation(input_str)print(result)
这段代码将输出:我有一些标点符号比如
这样,你就可以删除字符串中的所有标点符号了。
#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) );
本文共计310个文字,预计阅读时间需要2分钟。
要将字符串中的所有标点符号删除,可以使用Python的字符串方法 `translate()` 结合 `str.maketrans()`。以下是一个简单的示例代码,它将删除字符串中的所有标点符号:
pythonimport string
def remove_punctuation(input_str): # 创建一个翻译表,将所有标点符号映射到None translator=str.maketrans('', '', string.punctuation) # 使用translate()方法删除标点符号 return input_str.translate(translator)
示例使用input_str=我有一些标点符号,比如!?,。result=remove_punctuation(input_str)print(result)
这段代码将输出:我有一些标点符号比如
这样,你就可以删除字符串中的所有标点符号了。
#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) );

