Stripping Punctuation
-
Anyone have an "efficient" routine for stripping punctuation for a string? Mike Stanbrook mstanbrook@yahoo.com
-
Anyone have an "efficient" routine for stripping punctuation for a string? Mike Stanbrook mstanbrook@yahoo.com
public string StripPunctuation(string source)
{
// any chars you want to keep, put between the [] brakets.
// i think the ^ has to remain where it is though.
string pattern = "[^a-zA-Z ]";// use regular expressions.
System.Text.RegularExpressions.Regex regex;
regex = new System.Text.RegularExpressions.Regex(pattern);
return regex.Replace(source, "");
}That should do it. I think .NET's regular expression classes are effiecient enough.
1001111111011101111100111100101011110011110100101110010011010010
Sonork | 100.21142 | TheEclypse -
public string StripPunctuation(string source)
{
// any chars you want to keep, put between the [] brakets.
// i think the ^ has to remain where it is though.
string pattern = "[^a-zA-Z ]";// use regular expressions.
System.Text.RegularExpressions.Regex regex;
regex = new System.Text.RegularExpressions.Regex(pattern);
return regex.Replace(source, "");
}That should do it. I think .NET's regular expression classes are effiecient enough.
1001111111011101111100111100101011110011110100101110010011010010
Sonork | 100.21142 | TheEclypseWhy not just rewrite:
string pattern = "[^a-zA-Z ]";
System.Text.RegularExpressions.Regex regex;
regex = new System.Text.RegularExpressions.Regex(pattern);
return regex.Replace(source, "");As
Regex regex = new Regex("[^a-zA-z]");
regex.Replace(source, "");Makes things a lot simpler if you skip the first string and move the declaration and instantiation on to one line.
I don't know whether it's just the light but I swear the database server gives me dirty looks everytime I wander past. -Chris Maunder Microsoft has reinvented the wheel, this time they made it round. -Peterchen on VS.NET
-
Why not just rewrite:
string pattern = "[^a-zA-Z ]";
System.Text.RegularExpressions.Regex regex;
regex = new System.Text.RegularExpressions.Regex(pattern);
return regex.Replace(source, "");As
Regex regex = new Regex("[^a-zA-z]");
regex.Replace(source, "");Makes things a lot simpler if you skip the first string and move the declaration and instantiation on to one line.
I don't know whether it's just the light but I swear the database server gives me dirty looks everytime I wander past. -Chris Maunder Microsoft has reinvented the wheel, this time they made it round. -Peterchen on VS.NET
David Stone wrote: Regex regex = new Regex("[^a-zA-z]");regex.Replace(source, ""); or
return new Regex("[^a-zA-z]").Replace(source, "");
:) WebBoxes - Yet another collapsable control, but it relies on a "graphics server" for dynamic pretty rounded corners, cool arrows and unlimited font support. -
David Stone wrote: Regex regex = new Regex("[^a-zA-z]");regex.Replace(source, ""); or
return new Regex("[^a-zA-z]").Replace(source, "");
:) WebBoxes - Yet another collapsable control, but it relies on a "graphics server" for dynamic pretty rounded corners, cool arrows and unlimited font support.Bah, it's all the same IL-wise. :-D
I don't know whether it's just the light but I swear the database server gives me dirty looks everytime I wander past. -Chris Maunder Microsoft has reinvented the wheel, this time they made it round. -Peterchen on VS.NET