/// <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; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter