Saturday, July 2, 2011

Get first letter of each word in a sentence


 private string GetAcronym(string strSiteName)
        {
            string shortName = string.Empty;
            string[] txtArray = strSiteName.Split(' ');
     
       //if site name is a single word, get the first three characters of the site name
                if (txtArray.Length == 1)
                {
                    return strSiteName.Substring(0, 3);
                }

                //Get the first letter of each word in the site name
                foreach (string word in txtArray)
                {
                    shortName += word.Substring(0, 1);
                }
                return shortName;
            }