Home
C#
String Remove Punctuation
Updated Aug 21, 2024
Dot Net Perls
Punctuation. Often we find the C# Trim() method has limitations. It can handle groups of characters, but these must be specified explicitly.
Requirement info. A requirement is to remove punctuation from the starts and ends of strings. With a custom method we avoid the complexity of Trim.
String Trim
Example. We introduce TrimPunctuation, which has 2 loops. Each loop uses char.IsPunctuation. When a non-punctuation character is encountered, they stop iterating, with the break keyword.
char
break
Start The for-loop counts the number of punctuation characters at the start of the string.
for
And The next for-loop counts the punctuation at the end. It iterates in the reverse order.
Return If no punctuation was found, the method returns the original string. If all characters were punctuation, it returns an empty string.
Info The method returns a substring with the punctuation characters removed in the final case.
string.Empty
String Substring
using System; class Program { static void Main() { string[] array = { "Do you like this site?", "--cool--", "...ok!", "None", "" }; // Call method on each string. foreach (string value in array) { Console.WriteLine(Program.TrimPunctuation(value)); } } /// <summary> /// TrimPunctuation from start and end of string. /// </summary> static string TrimPunctuation(string value) { // Count start punctuation. int removeFromStart = 0; for (int i = 0; i < value.Length; i++) { if (char.IsPunctuation(value[i])) { removeFromStart++; } else { break; } } // Count end punctuation. int removeFromEnd = 0; for (int i = value.Length - 1; i >= 0; i--) { if (char.IsPunctuation(value[i])) { removeFromEnd++; } else { break; } } // No characters were punctuation. if (removeFromStart == 0 && removeFromEnd == 0) { return value; } // All characters were punctuation. if (removeFromStart == value.Length && removeFromEnd == value.Length) { return ""; } // Substring. return value.Substring(removeFromStart, value.Length - removeFromEnd - removeFromStart); } }
Do you like this site cool ok None
Regex. The string manipulation here could be done with Regex. And the code would even be shorter. It would also be slower. This is not a big consideration for some programs.
Regex.Match
A discussion. This method could be useful for handling the forms at the back-end of a website. It could help with handling invalid fields in a database.
Also If you have a specific requirement, such as removing all punctuation and also all numbers, this sort of solution can come in handy.
A summary. We developed a simple looping method that strips leading and trailing punctuation from a string. A Regex could be used, but it would be comparatively more complex.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Aug 21, 2024 (edit).
Home
Changes
© 2007-2025 Sam Allen