Usuário com melhor resposta
Putting lastname 1st one the string array

Pergunta
-
Hi!
I have : string[] LettersArray with names in it.
Example : " Juan Pedro Rodrigues Sanchez" , " Juan Perez Rodrigues", "Jose Antonio Fernando Rodriguez Perez"
And i need to get the last name and put it first.
For example :" Sanchez, Juan Pedro Rodrigues" , "Rodrigues, Juan Perez", "Perez , Jose Antonio Fernando Rodriguez".
I wanted to make a method for it.
Respostas
-
Nesse seu arquivo nomes.txt já tem os nomes completos ou tem mais algo que você não quer?
Talvez tenha alguma string que tenha ficado vazia, mude o código do método para:
public static string[] OrganizeNames(string[] input) { List<string> result = new List<string>(); input.ToList().ForEach(x => { string[] a = x.Split(' '); if (a.Count() > 1) result.Add($"{a[a.Count() - 1]}, {x.Replace(a[a.Count() - 1], "").Trim()}"); }); return result.ToArray(); }
Se a resposta for relevante ou tenha resolvido seu problema, marque como útil/resposta!
André Secco
Microsoft MSP & MSDN Tech Advisor
Blog: http://andresecco.com.br
GitHub: http://github.com/andreluizsecco
Twitter: @andre_secco- Marcado como Resposta luisjesusrocha terça-feira, 29 de novembro de 2016 13:38
Todas as Respostas
-
Olá,
Olha:
public string[] OrganizeNames(string[] input) { List<string> result = new List<string>(); input.ToList().ForEach(x => { string[] a = x.Split(' '); result.Add($"{a[a.Count() - 1]}, {x.Replace(a[a.Count() - 1], "").Trim()}"); }); return result.ToArray(); }
Use isso para chamar o método:
string[] input = { "Juan Pedro Rodrigues Sanchez", "Juan Perez Rodrigues", "Jose Antonio Fernando Rodriguez Perez" }; string[] result = OrganizeNames(input);
Se a resposta for relevante ou tenha resolvido seu problema, marque como útil/resposta!
André Secco
Microsoft MSP & MSDN Tech Advisor
Blog: http://andresecco.com.br
GitHub: http://github.com/andreluizsecco
Twitter: @andre_secco- Editado André SeccoMVP terça-feira, 29 de novembro de 2016 10:56
-
-
Luis, você pode falar em português hehe.
O método que enviei já traz o array como você quer
Se a resposta for relevante ou tenha resolvido seu problema, marque como útil/resposta!
André Secco
Microsoft MSP & MSDN Tech Advisor
Blog: http://andresecco.com.br
GitHub: http://github.com/andreluizsecco
Twitter: @andre_secco -
-
O erro é porque você está querendo chamar ele diretamente, porém ele não é um método estático. Se quiser chamar dessa forma, deixe o método assim:
public static string[] OrganizeNames(string[] input) { List<string> result = new List<string>(); input.ToList().ForEach(x => { string[] a = x.Split(' '); result.Add($"{a[a.Count() - 1]}, {x.Replace(a[a.Count() - 1], "").Trim()}"); }); return result.ToArray(); }
Se a resposta for relevante ou tenha resolvido seu problema, marque como útil/resposta!
André Secco
Microsoft MSP & MSDN Tech Advisor
Blog: http://andresecco.com.br
GitHub: http://github.com/andreluizsecco
Twitter: @andre_secco -
-
Poste seu código. Você deve estar passando um array vazio para o método.
Se a resposta for relevante ou tenha resolvido seu problema, marque como útil/resposta!
André Secco
Microsoft MSP & MSDN Tech Advisor
Blog: http://andresecco.com.br
GitHub: http://github.com/andreluizsecco
Twitter: @andre_secco -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string[] list_original = File.ReadAllLines("nomes.txt");
List<string> NumbersLst = new List<string>();
List<string> LettersLst = new List<string>();
foreach (String content in list_original)
{
//Retrieves the number
var getNumbers = (from t in content
where char.IsDigit(t)
select t).ToArray();
//Retrieves the string
var getLetters = (from t in content
where (char.IsLetter(t) || char.IsSeparator(t))
select t).ToArray();
string Number = new string(getNumbers);
string Letters = new string(getLetters);
NumbersLst.Add(Number);
LettersLst.Add(Letters);
}
//Convert Lists to arrays
string[] NumbersArray = NumbersLst.ToArray();
string[] LettersArray = LettersLst.ToArray();
string[] input = LettersArray;
string[] result = OrganizeNames(input);
}
public static string[] OrganizeNames(string[] input)
{
List<string> result = new List<string>();
input.ToList().ForEach(x =>
{
string[] a = x.Split(' ');
result.Add($"{a[a.Count() - 1]}, {x.Replace(a[a.Count() - 1], "").Trim()}");
});
return result.ToArray();
}
}
} -
Nesse seu arquivo nomes.txt já tem os nomes completos ou tem mais algo que você não quer?
Talvez tenha alguma string que tenha ficado vazia, mude o código do método para:
public static string[] OrganizeNames(string[] input) { List<string> result = new List<string>(); input.ToList().ForEach(x => { string[] a = x.Split(' '); if (a.Count() > 1) result.Add($"{a[a.Count() - 1]}, {x.Replace(a[a.Count() - 1], "").Trim()}"); }); return result.ToArray(); }
Se a resposta for relevante ou tenha resolvido seu problema, marque como útil/resposta!
André Secco
Microsoft MSP & MSDN Tech Advisor
Blog: http://andresecco.com.br
GitHub: http://github.com/andreluizsecco
Twitter: @andre_secco- Marcado como Resposta luisjesusrocha terça-feira, 29 de novembro de 2016 13:38