Home > database >  Javascript split slice join string
Javascript split slice join string

Time:12-14

I am struggling a bit with this problem.

I have a string "Jan van Wyk". "Jan" is the firstname and "van Wyk" is the lastname.

let enqBy = "Jan van Wyk";
var firstName = enqBy.split(' ').slice(0, -1).join(' ');
var lastName = enqBy.split(' ').slice(-1).join(' ');
console.log(firstName);
console.log(lastName);

Using this code the result is firstname = "Jan van Wyk" and lastname = "" which is not what I want.

If I have a string like "Elvis Presly" the code works perfectly firstname = "Elvis" and lastname = "Presley".

Please guide me.

CodePudding user response:

As you state in your question: "Jan" is the firstname and "van Wyk" is the lastname.

Then you want to pick the first item for firstname and the rest for lastname, this code would do:

const [firstname, ...rest] = enqBy.split(' ')
const lastname = rest.join(' ')

CodePudding user response:

Your Code seems to work on my side, Hope I got your to question well but just adjusted the first name to the first string after the split and joined the last two with a space.

const enqBy = "Jan van Wyk"

var firstName = enqBy.split(' ')[0]; 
var lastName = enqBy.split(' ').slice(1).join(' ');

console.log(firstName)
console.log(lastName)

// Output
// "Jan"
// "van Wyk"

CodePudding user response:

The issue with your code is that you are using the slice method to try to split the string into first and last name, but this method is not well-suited for this task. Instead, you can use the split method to split the string into an array of individual words, and then use array indexing to access the first and last elements of the array to get the first and last names.

let enqBy = "Jan van Wyk";
let words = enqBy.split(' ');
let firstName = words[0];
let lastName = words[words.length - 2]   ' '  words[words.length - 1];

console.log(firstName); // Output: "Jan"
console.log(lastName); // Output: "van Wyk"

CodePudding user response:

Use this code below. This also works for names like "Elvis Presly" that have one single word for last name .

var enqBy = "Jan van Wyk";var firstName = enqBy.split(" ").slice(0, 1).join(" ");var lastName = enqBy.split(" ").slice(1).join(" ");

CodePudding user response:

To get the first name of sentence, get the split using the space and then to get lastname get the length of sentence and slice.

let abc = "Praveen Ash Surya"
var fname = abc.split(' ', 1).join(' ');
var lname = abc.split(' ', abc.length).slice(1).join(' ');
console.log(fname);
console.log(lname);

Split can be used with separator and limit

lastname.split(seperator, ?limit)

CodePudding user response:

You can try this code:

const fullName = "Jan van Wyk"
const regex = /^(\w \s)(.*)$/g
const matches = regex.exec(fullName)
matches.shift() // remove first value which is the fullName
const [firstName, lastName] = matches

console.log(firstName.trim()) // Jan
console.log(lastName.trim()) // van Wyk

The first group this regex matches is the name (some letters space) second group is the rest of the fullName.

  • Related