aftab
aftab

Reputation: 693

How to get substring from string?

I'm trying to get the last word from the string:

var str =  "Member/GetRefills";
var param = str.split(" ").pop();
console.log(param);

Expected result:

GetRefills

Upvotes: 1

Views: 202

Answers (2)

Evik Ghazarian
Evik Ghazarian

Reputation: 1791

Do this instead:

var param = str.split('/');
var paramLast = param[param.length - 1];

pop() is not always a good practice. pop() removes the last element of an array which in some cases might cause problems.

param[param.length - 1] does the same job as pop() but more safely.

Upvotes: 0

ray
ray

Reputation: 27285

Try splitting on "/" instead.

const str = "Member/GetRefills";
const param = str.split("/").pop();

Upvotes: 7

Related Questions