Regex help Again

  • Thread starter Thread starter barry
  • Start date Start date
B

barry

Hi

1 str = "Hello World !!!"

kindly help in removing extra spaces from the above text

2. str = "THE QUICK BLACK FOX JUMPED OVER THE LAZY DOG"

and to change first letter of above to caps.

TIA
Barry
 
Hello Barry,
Hi

1 str = "Hello World !!!"

kindly help in removing extra spaces from the above text

2. str = "THE QUICK BLACK FOX JUMPED OVER THE LAZY DOG"

and to change first letter of above to caps.


1)

Regex.Replace(str, @"\s{2,}", "", RegexOptions.None);

2) This line will do the trick

string s = Regex.Replace(str, @"\w+", new MatchEvaluator(this.MakeTitleCase),
RegexOptions.None);

You also need this function

private string MakeTitleCase(Match m)
{
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(m.Value.ToLower());
}
 
Back
Top