MID equivalent in Javascript


I have a list of people's name and dates in a string, each separated by a comma:

"john 01/01/2000, tom 01/07/2010"

Using IndexOf, I'm able to check a person's name against this list. But I want to return their date and not just whether they're on the list or not.

Using "john" as an example, my theory is that I get the length of the name "john", add 1 character to it and then use a MID equivalent function that will put the next 10 characters into a variable and this will be the date. My pseudocode in the last line needs to be fixed, can someone please help? Many thanks in advance
var people='john 01/01/2000, tom 01/07/2010';
var theposition = people.indexOf('john');

if (theposition >= 0 )		//name is found
var namelength = 'john'.length + 1;
var returndate = mid(people,(theposition + namelength),10);

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try substr function like below:

var people='john 01/01/2000, tom 01/07/2010'; 
var theposition = people.indexOf('john'); 
if (theposition >= 0 )          //name is found 
   var namelength = 'john'.length + 1; 
   var returndate = people.substr(theposition+namelength, 10);

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You mean something like substring?
people.substring(theposition + namelength, theposition + namelength+10);


You can write your own function and keep it in a library.  My version of the MID function is included below.

          str - the string we are LEFTing
          start - our string's starting position (0 based!!)
          len - how many characters from start we want to get

          returns: The substring from start to start+len
        function Mid(str, start, len)
                // Check 'start' and 'len' parameter validity
                if (start < 0 || len < 0) return "";

                var iEnd, iLen = String(str).length;
                if (start + len > iLen)
                        iEnd = iLen;
                        iEnd = start + len;

                return String(str).substring(start,iEnd);

kbitAuthor Commented:
Thanks for such fast replies, all answers worked but I used the first one (because it was first)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.