This is a convenient Javascript function that returns a value from a string of delimited values based upon the position index specified.
Example Usage:
getToken("steve@hotmail.com", 2, "@") //returns "hotmail.com"
getToken("123-45-6789", 2, "-") //returns "45"
getToken("first,middle,last", 3, ",") //returns "last"
Code:
function getToken(sVal, iIndex, sDelimiter)
{
if (sDelimiter.length > 0)
{
var aSubString = new Array();
aSubString = sVal.split(sDelimiter);
if (iIndex > aSubString.length
|| iIndex < 1)
{
return "";
}
else
{
return aSubString[iIndex - 1]
}
}
else
{
return "";
}
}