3

Given a string like this:

"boy, girl, dog, cat"

What would be a good way to get the first word and the rest of them, so I could have this:

var first_word = "boy";
var rest = "girl, dog, cat";

Right now I have this:

my_string.split(","); But that gives me all the words that are between the commas.

6 Answers 6

4

You can use both split and splice:

var str = "boy, girl, dog, cat";
var arr = str.split(",");
var fst = arr.splice(0,1).join("");
var rest = arr.join(",");

Or similar

// returns an array with the extracted str as first value
// and the rest as second value
function cutFirst(str,token){
   var arr = str.split(token);
   var fst = arr.splice(0,1);
   return [fst.join(""),arr.join(token)];
}
Sign up to request clarification or add additional context in comments.

1 Comment

these leave a leading space on the rest
1

Use a combination of substring() (returning the substring between two indexes or the end of the string) and indexOf() (returning the first position of a substring within another string):

var input = "boy, girl, dog, cat",
    pos = input.indexOf( ',' );

console.log( input.substring( 0, pos ) );
console.log( input.substring( pos + 1 ) );

Maybe you want to add an trim() call to the results to remove whitespaces.

Comments

0

You can use a regex to match the two strings before and after the first comma:

var v = "boy, girl, dog, cat";
var strings = v.match(/([^,]*),(.*)/);

Now strings will be a three element array where

  • strings[0] contains the full string
  • strings[1] contains the string before the first comma
  • strings[2] contains everything after the first comma

Comments

0

You can create an array in a lazy way and then retrieve just the first element.

var ArrayFromString = "boy, girl, dog, cat".split(",");
firstElement = ArrayFromString.pop();
alert(firstElement); //boy
alert(ArrayFromString); //girl, dog, cat​​​​​​​​​​​​​​​​​

Comments

0

Yet another option is to split the array, get first and join the rest:

var my_string = "boy, girl, dog, cat";
var splitted = my_string.split(',');
var first_item = splitted.shift();
var rest_of_the_items = splitted.join(',');

console.log(first_item); // 'boy'
console.log(rest_of_the_items); // 'girl, dog, cat'

2 Comments

Ik you posted this 10 years ago but i'd have been so helpful if you just had typed the output as a comment
Hey @Gabriel, I edited the answer. Lmk if it suits.
0
const my_string = "boy, girl, dog, cat"; 
const splitted = my_string.split(',');
splitted.forEach(element => console.log(element));

You can also them like an array:

console.log(\`This is a ${splitted\[0\]}\`);

// expected output: "boy"

// expected output: " girl"

// expected output: " dog"

// expected output: " cat"

1 Comment

This question is quite old and already has been answered, your answer is very similar to the already approved answer.

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.