Wednesday, May 14, 2008

StrToken/@Word in JavaScript


function strToken( string, separator, position ){
//If the string doesn't contain the separator, return
//empty string
if( string.indexOf( separator ) == -1 ){ return ''; }

var arr = string.split( separator );

//If the position is larger than the number of elements,
//return empty string
if( position > arr.length ){ return ''; }

//Return found item
return arr[ position - 1 ];
}



Tested in IE6/FF2

0 comments: