简体   繁体   English

如何获取一行中的最后一项

[英]how to get the last items in a row

I have string like Joe Doe Doe , owner business我有像 Joe Doe Doe 这样的绳子,老板生意

and I need to take from this string last name, first name, father name and his position.我需要从这个字符串中取出姓氏、名字、父亲的名字和他的职位。

var str = orgRequ.ValueName.Replace(",", "").Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    string LastName = str.Length > 0 ? str[0] : "";
                    string Name = str.Length > 1 ? str[1] : "";
                    string FatherName = str.Length > 2 ? str[2] : "";
string Positions=string.Join(" ", str.Reverse().Take(str.Count() - 3).ToArray());

but ran into a problem that the final position is written as System.Linq.Enumerable + d__75`1 [System.Char] instead of the owner business.但是遇到了一个问题,最终位置写为 System.Linq.Enumerable + d__75`1 [System.Char] 而不是所有者业务。 before insert into the database, i need again reverse Positions , how can this be cleaned using best practices在插入数据库之前,我需要再次反转 Positions ,如何使用最佳实践进行清理

Because you are reversing the str in string Positions you get Business Owner instead of Owner Business .因为您正在反转string Positionsstr ,所以您将获得Business Owner而不是Owner Business

So either you take str[3] + str[4] for a result of Owner Business: string Positions = str[3] + " " + str[4];因此,要么您将 str[3] + str[4] 作为所有者业务的结果: string Positions = str[3] + " " + str[4];

Or with your code, you have to reverse the two words (attention, quick and dirty):或者用你的代码,你必须reverse两个词(注意,快速和肮脏):

            string Positions = string.Join(" ", str.Reverse().Take(str.Count() - 3).ToArray());

            string output = "";
            string[] splitStrings = Positions.Split(' ');
            for (int i = splitStrings.Length - 1; i > -1; i--)
            {
                output = output + splitStrings[i] + " ";
            }
            Console.WriteLine("Result: " + output); //Owner Business

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

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