📌  相关文章
📜  从给定的字符串中删除标点符号

📅  最后修改于: 2022-05-13 01:57:08.849000             🧑  作者: Mango

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

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

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

例子:

Input : %welcome' to @geeksforgeek

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

C++
// CPP program to remove punctuation from a given string
 
#include 
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,@"[^\w\d\s]","");
     
    Console.Write(str);
}
}
 
// This code is contributed
// by 29AjayKumar


Javascript


输出:

Welcome to GeeksforGeeks