58. Removing punctuation

There are several approaches to this problem that you can take, including using LINQ or regular expressions. The following code simply loops through the string's characters to find those that are not punctuation:

// Return the string with punctuation removed.
public static string RemovePunctuation(this string input)
{
StringBuilder sb = new StringBuilder();
foreach (char ch in input)
if (!char.IsPunctuation(ch))
sb.Append(ch);
return sb.ToString();
}

The code loops through the characters, uses char.IsPunctuation to see which are punctuation, and adds those that are not punctuation to a StringBuilder. After it finishes its loop, the method returns the StringBuilder object's contents.

Download the RemovePunctuation example solution to see additional details.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset