To stay in the spirit of code sharing and showing of the plugin I found for putting source code in my blog post 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 InitCap(string tekst)
   if (string.IsNullOrEmpty(tekst) || tekst.Trim() == "")
      return tekst;
   string r ="";
   foreach (char c in tekst)
      r += r.Length == 0 || r[r.Length - 1] == ' '
           ? c.ToString().ToUpper()
           : c.ToString().ToLower();
   return r;

That’s it!

2 Comments on A string manipulater method; InitCap

  1. Mark says:

    I prefer TextInfo.ToTitleCase(string) 🙂

  2. Cees Alberts says:

    TextInfo.ToTitleCase() doesnt’t work on “all caps” words in a sentence. My method does! 😀

    Besides, how can I show the source code plugin if there is only one line being colorcoded! 😉

Leave a Reply