Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

JavaScript: String Tokenization and Substring (See related posts)

// Tokenize a comma-separated string
// then recreate the comma-separated list

  var currentTagTokens = currentTags.split( "," );
  var existingTags = "";

  for ( var i = 0; i < currentTagTokens.length; i++ )
  {
    existingTags = existingTags + currentTagTokens[ i ] + ", ";
  }

  // Remove the trailing ", " from existingTags
  existingTags = existingTags.substring( 0, existingTags.length - 3 ); 

Comments on this post

Venkman posts on Mar 01, 2007 at 07:47
As far as I know, Javascript Arrays have a join method...
alert( "one,two,three,four,five".split(",").join(", ") );
thitiv posts on Mar 29, 2007 at 00:59
Thanks, Venkman. That would help me eliminate the for loop.

You need to create an account or log in to post comments to this site.


Click here to browse all 4860 code snippets

Related Posts