# Thursday, January 19, 2006

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 "";
	}

}
Thursday, January 19, 2006 7:25:22 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Related posts:
.NET styled framework for Javascript
JavaScript trim() Function

All comments require the approval of the site owner before being displayed.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: b, blockquote@cite, em, i, strike, strong, sub, sup, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview