Strip punctuations from a String in C#
This article illustrates the different techniques to strip punctuations from a string in C#.
1. Using Char.IsPunctuation() method
The Char.IsPunctuation() method return true if the specified character is a punctuation mark; otherwise, false. The following code demonstrates the usage of the IsPunctuation() method with LINQ’s Where() method to strip punctuations from a string. This would require the System.Linq namespace.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Linq; public class Example { public static void Main() { String s = "a, b, c, d"; s = new string(s.Where(c => !char.IsPunctuation(c)).ToArray()); Console.WriteLine(s); // a b c d } } |
If you are not allowed to use LINQ, try using the below code. It creates an extension method to strip punctuations from a string, that uses a foreach loop to iterate over the string and collect all non-punctuation characters in a StringBuilder instance.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System; using System.Text; public static class StringExtension { public static string RemovePunctuations(this string input) { StringBuilder sb = new StringBuilder(); foreach (char c in input) { if (!char.IsPunctuation(c)) { sb.Append(c); } } return sb.ToString(); } } public class Example { public static void Main() { String s = "a, b, c, d"; s = s.RemovePunctuations(); Console.WriteLine(s); // a b c d } } |
2. Using Regex.Replace method
Another option is to use a regular expression to strip all punctuation characters from a string. In C#, you can use the Regex.Replace() method for this. The following solution uses the !"#$%&'()*+,-./:;<=>?@\[\]^_`{|}~, which includes all US-ASCII punctuation characters. If there are any other characters that you want to remove, you can easily add them in this regex.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.Text.RegularExpressions; public static class StringExtension { public static string RemovePunctuations(this string input) { return Regex.Replace(input, "[!\"#$%&'()*+,-./:;<=>?@\\[\\]^_`{|}~]", string.Empty); } } public class Example { public static void Main() { String s = "(A,B#C::{D}[E])"; s = s.RemovePunctuations(); Console.WriteLine(s); // ABCDE } } |
The code can be shortened using the \p{P} character class, which matches with the US-ASCII punctuation by default.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.Text.RegularExpressions; public static class StringExtension { public static string RemovePunctuations(this string input) { return Regex.Replace(input, "\\p{P}", string.Empty); } } public class Example { public static void Main() { String s = "(A,B#C::{D}[E])"; s = s.RemovePunctuations(); Console.WriteLine(s); // ABCDE } } |
That’s all about stripping punctuations from a string in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)