2

what i am doing

for example i have a name

 var nameofuser = 'John Constantine vayo';
    var values = nameofuser.split(" ");
    var f_name = values[0];
    var l_name = nameofuser.substr(nameofuser.indexOf(' ') + 1);
    console.log(f_name);
    console.log(l_name);

it is working fine when user entering his last name and other title or so on, it is all going in l_name but when a user enterned only first name like only 'John' then l_name is also populated with 'john' :(,

i want IF a user type last name and so on then only l_name should be populated.

if user type only first name then only f_name with first name should be populated

6
  • What result do you want ? Commented Nov 24, 2016 at 11:16
  • if a user type last name and so on then only l_name should be populated. if user type only first name then only f_name with first name should be populated Commented Nov 24, 2016 at 11:17
  • It sounds like you want a script that recognizes whether a single word is a first name or a last name. How is it supposed to do that? Commented Nov 24, 2016 at 11:17
  • Check for the values.length Commented Nov 24, 2016 at 11:18
  • after first space everything should be in second variable Commented Nov 24, 2016 at 11:18

8 Answers 8

6
var nameofuser = 'John Constantine vayo';
var values = nameofuser.split(" ");
var f_name = values[0];
var l_name = values[1] ? nameofuser.substr(nameofuser.indexOf(' ') + 1) : '';
console.log(f_name);
console.log(l_name);
Sign up to request clarification or add additional context in comments.

2 Comments

If it works then please upvote my answer and check this as right answer @adasdasd
@Gauravjoshi You can look into values.join(' ') for l_name
1

Regular expressions are the usual tool for this kind of decomposition:

var nameofuser = str.match(/^\s*(\S+)\s*(.*?)\s*$/).slice(1);

'John Constantine vayo' gives ["John", "Constantine vayo"]

and 'John' gives ["John"].

But be careful of this kind of guesses. In a multicultural world it's safer to just take what the user provides.

2 Comments

I approve of this, but it might break if the string begins with a space. You just need to trim the string before matching it.
@alebianco I tuned the regex to do the trimming
1

When searching for a space over a string that doesn't have a space -1 would be returned. So -1 + 1 will be evaluated to 0 and the substring is returning the entire string.

var nameofuser = 'John Constantine vayo';
var values = nameofuser.split(" ");
var f_name = values.shift();
var l_name = values.length ? values.join(" ") : undefined;
console.log(f_name);
console.log(l_name);

3 Comments

You can skip ternary operation. Split will always return you an array of at least 1 value. Just use values.shift() for first name, and values.join(' ') for lastname
This will give you John and Constantine only. l_name should be Constantine vayo. You can check my answer, as I have already done that.
@Rajesh Yeah. I hope the current edit would address the issue that you were talking about.
0

It Is because You have to make sure there is more than one word for that line:

var l_name = nameofuser.substr(nameofuser.indexOf(' ') + 1);

Your code should look something like this:

var nameofuser = 'John Constantine vayo';
var values = nameofuser.split(" ");
var f_name = values[0];
var l_name = (values.length > 1) ? nameofuser.substr(nameofuser.indexOf(' ') + 1) : '';
console.log(f_name);
console.log(l_name);

Comments

0

Just check if there is more than one word in the values array? Hope this helps :)

var values = nameofuser.split(" ");
var f_name = values[0];
if (values.lenght >= 2) {
  var l_name = nameofuser.substr(nameofuser.indexOf(' ') + 1);
} else { //let lastname empty or do something else}

  console.log(f_name);
  console.log(l_name);
}

Comments

0

When you do .indexOf(), if value is not matches, you will get index as -1 so indexOf() + 1 would interpret as -1 + 1 = 0. So you end up doing namesofuser.substring(0)

Try something like this:

string.split + array.shift and array.join

function getNames(str) {
  var values = str.split(" ");
  var f_name = values.shift();
  var l_name = values.join(' ');
  console.log(f_name);
  console.log(l_name);
}

getNames('John Constantine vayo')
getNames("Foo")

string.substring

function getNames(str) {
  var index = str.indexOf(' ');
  index = index > -1 ? index : str.length;
  var f_name = str.substring(0, index);
  var l_name = str.substring(index + 1);
  console.log(f_name);
  console.log(l_name);
}

getNames('John Constantine vayo')
getNames("Foo")

Comments

0

var nameofuser = 'john';
var f_name = '';
var l_name ='';
var values = nameofuser.split(" ");
var f_name = values[0];
if(values.length > 1){
     var l_name = nameofuser.substr(nameofuser.indexOf(' ') +1);
  }       
console.log(f_name);
console.log(l_name);

Comments

0

this works as well and fixes the problems you'll have if nameofuser begins with a space

var nameofuser = 'John Constantine vayo';
var values = nameofuser.trim().split(" ");
var f_name = values[0];
var l_name = values.slice(1).join(" ")
console.log(f_name);
console.log(l_name);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.