c.Abbreviate

PHOTO EMBED

Wed Sep 22 2021 14:39:29 GMT+0000 (Coordinated Universal Time)

Saved by @rick_m #c#

/// <summary>
    /// Get a formatted abbreviation. 
    /// </summary>
    /// <param name="input">input string</param>
    /// <returns>shortened string ...</returns>
    public static string Abbreviate(string input)
    {
        string output = string.Empty;

        //Replace all special characters with spaces
        input = ReplaceSpecialCharactersWithSpaces(input.Trim());

        //Replace extra spaces with since space
        input = StripExtraSpaces(input);

        //ZIPasdf asdf R33b asdf

        char[] characters = input.ToCharArray();

        //for each word grab first letter. for numbers grab whole number
        bool keepLetter = true;
        foreach (char c in characters)
        {
            if (Char.IsNumber(c))
            {
                output += c.ToString();
                keepLetter = true;
            }
            else if (Char.IsWhiteSpace(c)) { keepLetter = true; }
            else if (Char.IsLetter(c) && Char.IsUpper(c))
            {
                output += c.ToString().ToUpper();
                keepLetter = false;
            }            
            else if (keepLetter && Char.IsLetter(c))
            {
                output += c.ToString().ToUpper();
                keepLetter = false;
            }
        }

        if (output.Length > 50)
        {
            output = output.Substring(0, 50);            
        }

        return output;
    }
content_copyCOPY