DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile


  • submit to reddit
using System.Text.RegularExpressions;

public class MyClass {

	public static void Main() {
		string text = "the quick red fox jumped over the lazy brown DOG.";
		System.Console.WriteLine("text=[" + text + "]");
		string result = Regex.Replace(text, @"\w+", new MatchEvaluator(MyClass.CapText));
		System.Console.WriteLine("result=[" + result + "]");

	static string CapText(Match m) {
		string temp = m.ToString();
		temp = char.ToUpper(temp[0]) + temp.Substring(1, temp.Length - 1).ToLower();
		return temp;



Snippets Manager replied on Wed, 2006/02/15 - 9:44pm

Thanks for the tip. Here's a shorter version: protected void Button1_Click(object sender, EventArgs e) { Label1.Text = Regex.Replace(TextBox1.Text, @"\b\w", new MatchEvaluator(stam)); } protected string stam(Match m) { return m.Value.ToUpper(); } (the trick is using \b which is a 0-length match of word boundaries, including ^ and \s)