Leo*_*ton 0 c# arrays string logic split
我最近获得了一个新的项目,将任何给定的字符串转换为1-3个字母的缩写.类似于我必须产生的东西的一个例子是下面给出的字符串可以是任何东西:
switch (string.Name)
{
case "Emotional, Social & Personal": return "ESP";
case "Speech & Language": return "SL";
case "Physical Development": return "PD";
case "Understanding the World": return "UW";
case "English": return "E";
case "Expressive Art & Design": return "EAD";
case "Science": return "S";
case "Understanding The World And It's People"; return "UTW";
}
Run Code Online (Sandbox Code Playgroud)
我想我可以使用string.Split并计算数组中的单词数.然后添加处理特定长度字符串的条件,因为这些句子通常不会长于4个字,但我将遇到的问题是.
关于我可以应用的逻辑的任何建议将非常感激.谢谢
类似下面的内容应该与您给出的示例一起使用.
string abbreviation = new string(
input.Split()
.Where(s => s.Length > 0 && char.IsLetter(s[0]) && char.IsUpper(s[0]))
.Take(3)
.Select(s => s[0])
.ToArray());
Run Code Online (Sandbox Code Playgroud)
您可能需要根据预期输入调整过滤器.可能会添加要忽略的单词列表.