Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to create acronym from upper letters in C#?

Tags:

c#

regex

acronym

What is the best way to create acronym from upper letters in C#?

Example:

Alfa_BetaGameDelta_Epsilon

Expected result:

ABGDE

My solution works, but it's not nice

        var classNameAbbreviationRegex = new Regex("[A-Z]+", RegexOptions.Compiled);
        var matches = classNameAbbreviationRegex.Matches(enumTypeName);
        var letters = new string[matches.Count];

        for (var i = 0; i < matches.Count; i++)
        {
            letters[i] = matches[i].Value;
        }

        var abbreviation = string.Join(string.Empty, letters);
like image 845
Rudis Avatar asked Aug 08 '13 12:08

Rudis


1 Answers

string.Join("", s.Where(char.IsUpper));
like image 140
Daniel Hilgarth Avatar answered Oct 16 '22 01:10

Daniel Hilgarth