C#获取DisplayName的首字母缩写

C#获取DisplayName的首字母缩写,c#,backend,dto,C#,Backend,Dto,我试图从显示名称中提取首字母,用于显示其首字母。 我发现这很难,因为字符串是一个包含一个或多个单词的值。我怎样才能做到这一点 例如: “约翰·史密斯”=>JS “史密斯,约翰”=>SJ “约翰”=>J “史密斯”=>S public static SearchDto ToSearchDto(this PersonBasicDto person) { return new SearchDto { Id

我试图从显示名称中提取首字母,用于显示其首字母。 我发现这很难,因为字符串是一个包含一个或多个单词的值。我怎样才能做到这一点

例如:

“约翰·史密斯”=>JS

“史密斯,约翰”=>SJ

“约翰”=>J

“史密斯”=>S

public static SearchDto ToSearchDto(this PersonBasicDto person)
        {
            return new SearchDto
            {
                Id = new Guid(person.Id),
                Label = person.DisplayName,
                Initials = //TODO: GetInitials Code
            };
        }
我使用了以下解决方案:我创建了一个helper方法,它允许我测试多个案例

public static string GetInitials(this string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return string.Empty;
            }

            string[] nameSplit = name.Trim().Split(new string[] { ",", " " }, StringSplitOptions.RemoveEmptyEntries);
            var initials = nameSplit[0].Substring(0, 1).ToUpper();

            if (nameSplit.Length > 1)
            {
                initials += nameSplit[nameSplit.Length - 1].Substring(0, 1).ToUpper();
            }

            return initials;
        }

那么以下内容如何:

   void Main()
{
    Console.WriteLine(GetInitials("John Smith"));
    Console.WriteLine(GetInitials("Smith, John"));
    Console.WriteLine(GetInitials("John"));
    Console.WriteLine(GetInitials("Smith"));
}

private string GetInitials(string name)
{
    if (string.IsNullOrWhiteSpace(name))
    {
        return string.Empty;
    }
    var splitted = name?.Split(' ');
    var initials = $"{splitted[0][0]}{(splitted.Length > 1 ? splitted[splitted.Length - 1][0] : (char?)null)}";
    return initials;
}
输出:


JS-SJ-J-S

简单易懂的代码,处理包含名字、中间名和姓氏的名称,如“John Smith William”


或者只是作为扩展方法的另一个变体,带有少量的健全性检查

给定的

public static class StringExtensions
{
   public static string GetInitials(this string value)
      => string.Concat(value
         .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
         .Where(x => x.Length >= 1 && char.IsLetter(x[0]))
         .Select(x => char.ToUpper(x[0])));
}
用法

var list = new List<string>()
{
   "James blerg Smith",
   "Michael Smith",
   "Robert Smith 3rd",
   "Maria splutnic Garcia", 
   "David Smith", 
   "Maria Rodriguez",
   "Mary Smith", 
   "Maria Hernandez"
};

foreach (var name in list)
   Console.WriteLine(name.GetInitials());
下面的代码来自。代码的作用是从字符串中提取每个单词的第一个字母,并将其作为大写字母输出

   static void printInitials(String name) 
    { 
        if (name.Length == 0) 
            return; 
  
        // Since touuper() returns int, 
        // we do typecasting 
        Console.Write(Char.ToUpper(name[0])); 
  
        // Traverse rest of the string and  
        // print the characters after spaces. 
        for (int i = 1; i < name.Length - 1; i++) 
            if (name[i] == ' '&((i + 1)!=name.Length)) 
                Console.Write(" " + Char.ToUpper(name[i + 1])); 
    } 

静态void打印首字母(字符串名称)
{ 
如果(name.Length==0)
返回;
//由于touper()返回int,
//我们打字
Console.Write(Char.ToUpper(名称[0]);
//遍历字符串的其余部分并
//打印空格后的字符。
for(inti=1;i
您是否假设总是在空格字符后使用第一个字母?你能具体说明任何(其他)这样的假设吗?(在空格的情况下,典型的模式是“拆分,取每个元素的第一个字符,连接”。)放在一边:。谢谢!我使用了这个答案中的一些代码,并对其进行了更改以处理我的问题。
JBS
MS
RS
MSG
DS
MR
MS
MH
   static void printInitials(String name) 
    { 
        if (name.Length == 0) 
            return; 
  
        // Since touuper() returns int, 
        // we do typecasting 
        Console.Write(Char.ToUpper(name[0])); 
  
        // Traverse rest of the string and  
        // print the characters after spaces. 
        for (int i = 1; i < name.Length - 1; i++) 
            if (name[i] == ' '&((i + 1)!=name.Length)) 
                Console.Write(" " + Char.ToUpper(name[i + 1])); 
    }