Search code examples
c#regexacronym

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


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);

Solution

  • string.Join("", s.Where(char.IsUpper));