简体   繁体   中英

how to get the last items in a row

I have string like Joe Doe Doe , owner business

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. before insert into the database, i need again reverse Positions , how can this be cleaned using best practices

Because you are reversing the str in string Positions you get Business Owner instead of Owner Business .

So either you take str[3] + str[4] for a result of Owner Business: string Positions = str[3] + " " + str[4];

Or with your code, you have to reverse the two words (attention, quick and dirty):

            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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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