Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a whole string of values separated by a delimiter(e.g. space) like:
Hello World How Are You
My codes are as below:

var param = _arrGlobal.substring(_arrGlobal.lastIndexOf(' ') + 1);
alert(param);

How can I retrieve the word "You" which is after the last delimiter?
I have tried indexOf() or lastIndexOf() but cannot really do it.

What I have tried:

1. Tried indexOf() and lastIndexOf().
Posted
Updated 3-Aug-16 17:16pm
Comments
Jamie888 3-Aug-16 22:24pm    
I have tried to inspect. Error saying substring is not a method.
Patrice T 3-Aug-16 22:31pm    
Use Improve question to update your question.
Jamie888 4-Aug-16 1:31am    
Found out to be missing conversion for the _arrGlobal.
var param1 = _arrGlobal.toString();
var param = param1.substring(param1.lastIndexOf(' ') + 1);
alert(param);

1 solution

try this

JavaScript
var text = 'Hello World How Are You'
       var array = text.split(' ');
       var data = array[array.length - 1]; // you
 
Share this answer
 
Comments
Jamie888 4-Aug-16 1:23am    
Thank you sir for your info. I have found the root cause error on the above question. It is due to I must convert the _arrGlobal into string(e.g. _arrGlobal.toString()) before substring it. Then the error message would disappear. Anyway, I will mark your answer as solution here. Thank you again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900