Change name and surname in a string

Asked

Viewed 129 times

0

I work with an API that returns the last name to me first after the name and I need the reverse as the name reads ( in my case is a Book Author ), the return I have is ( Ex.: Bragança, Luiz Philippe De Orleans E ) and I need "Luiz Philippe De Orleans E Bragança", I tried to do with Verse but only works with Authors with 2 words.

string.Join(" ", Model.ItemInfo.ByLineInfo.Contributors[0].Name.Split(' ',',').Reverse())
  • That one name comes Bragança, Luiz Philippe De Orleans E?

  • Hello Felipe, yes this way, and I need how to read the author "Luiz Philippe De Orleans E Bragança"

1 answer

1


It seems that you want to put the first word at the end of what is returned. So, you can:

public static string NomeAutor(string autor) {
    if(autor.Trim() == "") return "";
    string nomeCompletoRetornado = autor;
    string primeiroNome = nomeCompletoRetornado.Split(new char[] {' ', ','})[0].Trim();
    string resto = nomeCompletoRetornado.Substring(
         primeiroNome.Length + 1,
         nomeCompletoRetornado.Length - primeiroNome.Length - 1).Trim();
    string resultadoFinal = resto + " " + primeiroNome + ".";
    return resultadoFinal;
}

Use:

NomeAutor("Bragança, Luiz Philippe De Orleans E") => "Luiz Philippe De Orleans E Bragança."
NomeAutor("José, Fino de Souza Pereira") => "Fino de Souza Pereira José."

See working on .NET Fiddle.

This will take the full name, get the first name before the ',' or ' ', remove from the original name the size of what was obtained from the first result and briefly concatenate it at the end.

  • It would not just be the case to split the comma and reverse the positions?

  • @Felipeavelar depending on the input, may be being divided without comma as well. From what I understood in his code is this, it would not make sense to use the ' ' and the ,. In this case, if you need to divide by spaces, you would divide the other words as well.

  • 1

    Thanks @Cypherpotato was just that, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.