Take name and surname of a string

Asked

Viewed 7,927 times

4

I need to take the name and surname of a string. For example, if the full name is:

Renan Rodrigues Moraes

I just need to take Renan Rodrigues. I know what to do but I don’t know how to do it. In case the name is Renan de Assis it would be interesting to return Renan de Assis.

I know what I must do but I don’t know how.

The main idea is to count two spaces, from the second showing nothing. Someone has a suggestion to extract this from the string?

  • Separates your string by spaces and takes the 1 and 2 part.

5 answers

9


You can use .split(' ') to create an array. Then using the .slice(0, 2) create a copy only with the first two. Finally, if you want back a string, you can do .join(' '):

'Renan Rodrigues Moraes'.split(' ').slice(0, 2).join(' ');

example: https://jsfiddle.net/bo2ropp1/

In case there are names with de or do as an example Renan de Assis you could use regex like this (link), or check if the second word starts with big letter like this (link), thus:

function nome(str) {
    var arr = str.split(' ');
    if (arr[1][0].toUpperCase() != arr[1][0]) arr.splice(1, 1);
    return arr.slice(0, 2).join(' ');
}

Anyway you need to create some examples and test to make sure it works as you want...

And to keep these de/do you could do so:

function nome(str) {
    var arr = str.split(' ');
    var keep = arr[1][0].toUpperCase() != arr[1][0];
    return arr.slice(0, keep ? 3 : 2).join(' ');
}

jsFiddle: https://jsfiddle.net/6ezhLo0f/1/

  • And in case the name is Renan de Assis? how would you look ?

  • @Renanrodrigues in that case you want alone Renan Assis return?

  • The interesting thing would be Renan de Assis

  • @Renanrodrigues I see, I put it in the answer.

  • The problem is to ensure that in the register the de, da or do are not capitalized. I thought about considering the number of characters but would give problems in names like Antônio de Sá. Maybe your role can really ignore the conditions de, da and do since they are unique, I believe.

  • @Gerep is true. That’s why I referred in the answer "create some examples and test to make sure it works as you want". It is possible to make a more complex version but without knowing more where the data comes from and possible formats I think the answer is correct what is requested.

  • 1

    @Sergio Entendi. I ended up adding an answer with a more direct and less dynamic solution.

Show 2 more comments

4

You can split the whitespace of the string, then take only the first and second element of the array:

var nome = "Renan Rodrigues Moraes";
var tmp = nome.split(" ");
nome = tmp[0] + " " + tmp[1];

Edit:

But you will need special treatment for cases where the name is type "João da Silva", because in this case would be "João da".

  • How would you look with treatment ?

  • while ? for ? would you add in your example ?

  • If you are looking for the full name from the bank, the best way is always to differentiate the two information by saving in different columns ( name / surname ).

  • I think the @Sergio answer solves this problem.

3

Taking into account my comment on Sergio’s reply...

The problem is to ensure that in the register the of, of or of are not written in capital letters. I thought about considering the number of characters but it would give problems in names like Antônio de Sá. Perhaps your function can really ignore the conditions of, of and since they are unique, I believe.

here is a possible solution:

function nome(str) {
    var arr = str.split(' ');
    if(arr[1].toLowerCase() == 'de' || arr[1].toLowerCase() == 'da' || arr[1].toLowerCase() == 'do') {
        return arr[0] + " " + arr[1] + " " + arr[2]
    } else {
        return arr[0] + " " + arr[1]
    }
}

console.log(nome('Renan de Rodrigues Moraes'));
console.log(nome('Renan DE Rodrigues Moraes'));
console.log(nome('Antônio De Sá Moreira'));
  • +1, but it shouldn’t be return arr[0] + " " + arr[1] + " " + arr[2] in time of return arr[0] + " " + arr[2]?

  • @Sergio In this case the arr[1] would be the de, da or do, I believe the goal is to ignore them?

  • The idea is to keep these as well: http://answall.com/questions/125559/pega-first-name-and-last-of-a-string/125562?noredirect=1#comment261750_125562

  • 2

    Suggestion: ['de', 'da', 'do', 'das', 'dos'].indexOf(arr[1].toLowerCase()) > -1. And I also understood that it is to keep. That is, return the first three positions if there is a connector, otherwise return the first two.

2

You can use JS Split to take the name and surname of the string from its spaces.

Would look like this:

var nomeCompleto = "Renan Rodrigues Moraes";
var nome = nomeCompleto.split(" ")[0];
var sobrenome = nomeCompleto.split(" ")[1];

0

Digging up a topic...

I needed it now, I needed a first and last name:

    var nomeCompleto = "Renan Rodrigues Moraes";
    var nome = nomeCompleto.split(" ")[0];
    var qtdnome = nomeCompleto.split(" ").length;
    var sobrenome = nomeCompleto.split(" ")[qtdnome-1]; 
    console.log(qtdnome)
    console.log(nome)
    console.log(sobrenome)

Browser other questions tagged

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