删除给定字符串中的标点符号

给定一个字符串,如果给定字符是按当前C语言环境分类的标点字符,请从字符串中删除标点。默认C语言环境将这些字符分类为标点:

null
!"#$%&'()*+,-./:;?@[]^_`{|}~ 

例如:

Input : %welcome' to @geeksforgeek<sOutput : welcome to geeksforgeeksInput : Hello!!!, he said ---and went.Output : Hello he said and went

设计了一个循环,它遍历由该字符串的字符和标点组成的列表,删除标点,然后将它们连接起来。

C++

// CPP program to remove punctuation from a given string
#include <iostream>
using namespace std;
int main()
{
// input string
std::string str = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks" ;
for ( int i = 0, len = str.size(); i < len; i++)
{
// check whether parsing character is punctuation or not
if (ispunct(str[i]))
{
str.erase(i--, 1);
len = str.size();
}
}
// print string without punctuation
std::cout << str;
return 0;
}


JAVA

// Java program to remove punctuation from a given string
public class Test
{
public static void main(String[] args)
{
// input string
String str = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks" ;
// similar to Matcher.replaceAll
str = str.replaceAll( "\p{Punct}" , "" );
System.out.println(str);
}
}
// This code is contributed by Gaurav Miglani


Python3

# Python program to remove punctuation from a given string
# Function to remove punctuation
def Punctuation(string):
# punctuation marks
punctuations = '''!()-[]{};:'",<>./?@#$%^&*_~'''
# traverse the given string and if any punctuation
# marks occur replace it with null
for x in string.lower():
if x in punctuations:
string = string.replace(x, "")
# Print string without punctuation
print (string)
# Driver program
string = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks"
Punctuation(string)


C#

// C# program to remove punctuation
// from a given string
using System;
using System.Text.RegularExpressions;
class GFG
{
public static void Main()
{
// input string
String str = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks" ;
// similar to Matcher.replaceAll
str = Regex.Replace(str, @"[^wds]" , "" );
Console.Write(str);
}
}
// This code is contributed
// by 29AjayKumar


Javascript

<script>
// JavaScript program to remove punctuation from a given string
{
// input string
var str = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks" ;
// similar to Matcher.replaceAll
str = str.replace(/[^a-zA-Z ]/g, "" );
document.write(str);
}
// This code is contributed by shivanisingh
</script>


输出:

Welcome to GeeksforGeeks

本文由 普拉莫德·库马尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享