C# Remove Punctuation From StringRemove punctuation from a string with a loop that trims leading and trailing characters.
Trim() has limitations. It can handle groups of characters, but these must be specified explicitly. A requirement is to remove punctuation from the starts and ends of strings. With a custom method we avoid the complexity of Trim.Trim
In this example
we introduce a method called TrimPunctuation. It contains two loops. Each of these loops uses the char.IsPunctuation method. When a non-punctuation character is encountered, they stop iterating, with the break keyword.CharBreak
First loop: This counts the number of punctuation characters at the start of the string.
Second loop: This loop counts the punctuation at the end. It iterates in the reverse order.
Info: If no punctuation was found, the method returns the original string. If all characters were punctuation, it returns an empty string.
And: The method returns a substring with the punctuation characters removed in the final case.Empty StringSubstring
C# program that trims punctuation characters
static void Main()
string array =
"Do you like this site?",
// Call method on each string.
foreach (string value in array)
/// TrimPunctuation from start and end of string.
static string TrimPunctuation(string value)
// Count start punctuation.
int removeFromStart = 0;
for (int i = 0; i < value.Length; i++)
// Count end punctuation.
int removeFromEnd = 0;
for (int i = value.Length - 1; i >= 0; i--)
// No characters were punctuation.
if (removeFromStart == 0 &&
removeFromEnd == 0)
// All characters were punctuation.
if (removeFromStart == value.Length &&
removeFromEnd == value.Length)
value.Length - removeFromEnd - removeFromStart);
Do you like this site
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 most programs. But for important programs, performance is often more concerning.Regex
Much of computer programming involves string manipulation. 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.
Note: A regex could still be used, but it will become comparatively more complex.
Some of the .NET Framework methods have serious limitations. They are useful for a narrow purpose, but if your requirements exceed this, you have a problem. But any framework that has every possible method in it would be unwieldy.
Review: We developed a simple looping method that strips leading and trailing punctuation from a string.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to firstname.lastname@example.org.