Returns the initials of a name or sentence - CSharp System

CSharp examples for System:String Case

Description

Returns the initials of a name or sentence

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Globalization;
using System.Collections.Generic;
using System;//from w w w .  ja v a  2s . c o  m

public class Main{
        // Returns the initials of a name or sentence
        //capitalize - whether to make intials capitals
        //includeSpace - to return intials separated (True - J. S. or False - J.S.)
        //John Smith -> J. S. or J.S.
        public static string GetInitials(string input, bool capitalize, bool includeSpace)
        {
            string[] words = input.Split(new char[] { ' ' });

            for (int i = 0; i < words.Length; i++)
            {
                if (words[i].Length > 0)
                    if (capitalize)
                        words[i] = words[i].Substring(0, 1).ToUpper() + ".";
                    else
                        words[i] = words[i].Substring(0, 1) + ".";
            }

            if (includeSpace)
                return String.Join(" ", words);
            else
                return String.Join("", words);
        }
}

Related Tutorials