DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

JavaScript: String Tokenization And Substring

03.01.2007
| 53509 views |
  • submit to reddit
        // 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

Snippets Manager replied on Wed, 2006/08/02 - 11:16am

Thanks, Venkman. That would help me eliminate the for loop.

Peter Venkman replied on Thu, 2007/03/01 - 8:42am

As far as I know, Javascript Arrays have a join method... alert( "one,two,three,four,five".split(",").join(", ") );