How can I strip punctuation from a string?

前端 未结 15 557
天命终不由人
天命终不由人 2020-12-04 18:47

For the hope-to-have-an-answer-in-30-seconds part of this question, I\'m specifically looking for C#

But in the general case, what\'s the best way to strip punctuati

15条回答
  •  鱼传尺愫
    2020-12-04 19:19

    For anyone who would like to do this via RegEx:

    This code shows the full RegEx replace process and gives a sample Regex that only keeps letters, numbers, and spaces in a string - replacing ALL other characters with an empty string:

    //Regex to remove all non-alphanumeric characters
    System.Text.RegularExpressions.Regex TitleRegex = new 
    System.Text.RegularExpressions.Regex("[^a-z0-9 ]+", 
    System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    
    string ParsedString = TitleRegex.Replace(stringToParse, String.Empty);
    
    return ParsedString;
    

提交回复
热议问题