Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the last String token with Javascript

Tags:

javascript

How do I implement a String tokenizer and get the last token of a String separated with comma "," using javascript?

The String to be tokenized may get very large, and that it needs to be memory efficient so the browser won't hang.

like image 887
quarks Avatar asked Sep 05 '25 03:09

quarks


1 Answers

You can probably split the string on the separator and then reference the last item in the resulting array:

var tokenstring = "token1,token2,token3,token4",
    arr = tokenstring.split( ',' ),
    lastToken = arr.pop();  // .pop() removes and returns the last item of the array

console.log( lastToken );
like image 158
Kevin Boucher Avatar answered Sep 08 '25 01:09

Kevin Boucher