Sunday, April 3, 2011

A string manipulater method

To stay in the spirit of code sharing I decided to add another code snippet!

This little snippet of C# shows a method that will make all the first letters of the words in a string uppercase.

Okay, okay not exactly brain surgery but a nice snippet anyway! ;-)

public static string InitCapSmall(string tekst)
{
   if (string.IsNullOrEmpty(tekst) || tekst.Trim() == "") return tekst;
   string returnValue="";
   foreach (char c in tekst)
   {
      returnValue += returnValue.Length == 0 || 
                     returnValue[returnValue.Length - 1] == ' ' 
                     ? c.ToString().ToUpper() : 
                       c.ToString().ToLower();
   }
   return returnValue;
}
That's it!

I also put this blog on my site; www.developerblog.nl

No comments:

Post a Comment