简体   繁体   English

有没有更好的方法从C#中的大写字母创建首字母缩略词?

[英]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#? 在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);
string.Join("", s.Where(x => char.IsUpper(x))
string.Join("", s.Where(char.IsUpper));
string test = "Alfa_BetaGameDelta_Epsilon";
string result = string.Concat(test.Where(char.IsUpper));

You can use the Where method to filter out the upper case characters, and the Char.IsUpper method can be used as a delegate directly without a lambda expression. 您可以使用Where方法筛选出大写字符, Char.IsUpper方法可以直接用作委托而不使用lambda表达式。 You can create the resulting string from an array of characters: 您可以从一个字符数组创建结果字符串:

string abbreviation = new String(enumTypeName.Where(Char.IsUpper).ToArray());

By using MORE regexes :-) 通过使用更多正则表达式:-)

var ac = string.Join(string.Empty, 
                     Regex.Match("Alfa_BetaGameDelta_Epsilon", 
                                 "(?:([A-Z]+)(?:[^A-Z]*))*")
                          .Groups[1]
                          .Captures
                          .Cast<Capture>()
                          .Select(p => p.Value));

More regexes are always the solution, expecially with LINQ! 更多的正则表达式始终是解决方案,尤其是LINQ! :-) :-)

The regex puts all the [AZ] in capture group 1 (because all the other () are non-capturing group (?:) ) and "skips" all the non [AZ] ( [^AZ] ) by putting them in a non-capturing group. 正则表达式将所有[AZ]放在捕获组1中(因为所有其他()都是非捕获组(?:) )并且“跳过”所有非[AZ][^AZ] )将它们放入非捕获组。 This is done 0-infinite times by the last * . 这是由最后一次*完成0次无限次。 Then a little LINQ to select the value of each capture .Select(p => p.Value) and the string.Join to join them. 然后用一点LINQ来选择每次捕获的值。选择.Select(p => p.Value)string.Join加入它们。

Note that this isn't Unicode friendly... ÀÈÌÒÙ will be ignored. 请注意,这不是Unicode友好的...ÀÈÌÒÙ将被忽略。 A better regex would use @"(?:(\\p{Lu}+)(?:[^\\p{Lu}]*))*" where \\p{Lu} is the Unicode category UppercaseLetter. 更好的正则表达式将使用@"(?:(\\p{Lu}+)(?:[^\\p{Lu}]*))*"其中\\p{Lu}是Unicode类别UppercaseLetter。

(yes, this is useless... The other methods that use LINQ + IsUpper are better :-) but the whole example was built just to show the problems of Regexes with Unicode) (是的,这是无用的...使用LINQ + IsUpper的其他方法更好:-)但整个示例只是为了显示带有Unicode的Regex的问题而构建

MUCH EASIER: 更容易:

var ac = Regex.Replace("Alfa_BetaGameDelta_Epsilon", @"[^\p{Lu}]", string.Empty);

simply remove all the non-uppercase letters :-) 只需删除所有非大写字母:-)

var str = "Alfa_BetaGammaDelta_Epsilon";
var abbreviation = string.Join(string.Empty, str.Where(c => c.IsUpper()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM