Friday, May 1, 2009

Remove extra spaces from string using regular expression,, c#

public string RemoveExtaSpaces(string text)


Regex regex = new Regex(@”\s{2,}”, Options);

text = regex.Replace(text.Trim(), ” “); //This line removes extra spaces and make space exactly one.

//To remove the space between the end of a word and a punctuation mark used in the text we will

//be using following line of code

regex=new Regex(@”\s(\!|\.|\?|\;|\,|\:)”); // “\s” whill check for space near all puntuation marks in side ( \!|\.|\?|\;|\,|\:)”); )

text = regex.Replace(text, “$1″);

return text;


No comments:

Post a Comment