String Extension improvement I am creating a string extension to validate some input.

My scenario is when we have a string it will format it according to following guideline.
If the sample string is “One Two Three Four Five” and the length is &lt;=20 return same. If more than 20 then “One Two Three F Five”. If still length is more than 20 the “One Two T  F Five” etc...

Is there any way I can improve this code? I thought it can use recursion?

public static string ToShortName(this string name)
        {
            if (name.Length &lt;= 20) return name;

            var namelist = name.Split(' ');

            var length = name.Length;

            //add spaces
            length = length + namelist.Count() - 1;

            var output = "";
            var indexList = new List&lt;int&gt;();
            for (var i = namelist.Count() - 2; i &gt;=1 ; i--)
            {
                indexList.Add(i);
                if (length - namelist[i].Length &lt;= 20)
                {
                    break;
                }
                length = length - namelist[i].Length;
            }

            foreach (var i in indexList)
            {
                namelist[i] = namelist[i].Substring(0, 1);
            }
            output = string.Join(" ", namelist);

            return output;
        }
You should remove this

length = length + namelist.Count() - 1;


because length already includes the spaces.

This 

length = length - namelist[i].Length;


should be

length -= namelist[i].Length - 1;


because namelist[i].Length-1 is the number of characters that are removed.

Also: why i &gt;=1 - 1 instead of i &gt;= 0?

You can also do it in a single loop like this:

        for (var i = namelist.Count() - 2; i &gt;=1 &amp;&amp; length &gt; 20; i--)
        {
            length -= namelist[i].Length - 1;
            namelist[i] = namelist[i].Substring(0, 1);
        }

        return string.Join(" ", namelist);