Ask Ben: Splitting Strings In Javascript To Form New Variables

Posted December 19, 2008 at 2:53 PM

Tags: Javascript / DHTML, Ask Ben

I have a variable (string) with a value of: "foobar|http://www.myurl.com". From that, I want to make two new variables:

label: foobar
url: http://www.myurl.com

How do I split it?

Before we can answer this, we have to think about where variables live. Variables in any programming language always reside within some sort of a scope. Sometimes this scope has a label (as in "window") and sometimes this scope has no label (as in the local scope of a function). In order for us to be able to create new variables dynamically, we are going to need a reference to the scope in which they will be created.

In Javascript, if you do not specify a variables scope, it defaults to "window" - the global scope. That being said, creating a new variable, most of the time, is the exact same as creating a new key in an object. For example, when creating a global Javascript variable:

 Launch code in new window » Download code as text file »

  • var objData = {};

... is essentially the same as this:

 Launch code in new window » Download code as text file »

  • window[ "objData" ] = {};

Notice that our variable name, "objData", is nothing more than a key with in the "window" object (global scope).

Sound good? Now, let's look at a demo. In the following code, I am abstracting this idea out into a method, SetVariables(), that takes care of scoping the new variables for you:

 Launch code in new window » Download code as text file »

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>Splitting Strings Into Variables</title>
  •  
  • <script type="text/javascript">
  •  
  • // This will take a string that is formatted as a delimited
  • // list of values and store those values into the given
  • // variable names in the given container.
  • function SetVariables(
  • strList, // The list of values.
  • arrLabels, // The array of new variable names.
  • strDelimiter, // OPTIONAL - defaults to "|".
  • objContainer // OPTIONAL - defaults to "window".
  • ){
  •  
  • // Check to see if there is a defined container.
  • // If not then default the container to window.
  • // This is basically where any "unscoped" variable
  • // resides naturally.
  • if (!objContainer){
  • objContainer = window;
  • }
  •  
  • // Check to see if a delmiter was passed. If not,
  • // then define a default one.
  • if (!strDelimiter){
  • strDelimiter = "|";
  • }
  •  
  • // Now that we have our values defaults, we are
  • // going to split the string into an array using
  • // the given delimiter. Escape the delimiter in case
  • // it's a special regEx value.
  • var arrValues = strList.split(
  • new RegExp( ("\\" + strDelimiter), "gi" )
  • );
  •  
  • // Loop over the values and assign them to the
  • // labels passed in. Do this in order. In the loop
  • // conidition, make sure that we stay in bounds for
  • // both the value array and the variable name array.
  • for (
  • var i = 0 ;
  • (
  • (i < arrValues.length) &&
  • (i < arrLabels.length)
  • );
  • i++
  • ){
  •  
  • // Store the new value in the container
  • objContainer[ arrLabels[ i ] ] = arrValues[ i ];
  •  
  • }
  •  
  • // No need to pass back container as it is an object
  • // and will be passed by reference; but, let's just
  • // pass it back for good measure.
  • return( objContainer );
  • }
  •  
  • </script>
  • </head>
  • <body>
  •  
  • <h1>
  • Splitting Strings Into Variables
  • </h1>
  •  
  • <!--- Let's do some testing. --->
  • <script type="text/javascript">
  •  
  • // For the first test, we are going to use the default
  • // delimiter and the default container (window).
  • SetVariables(
  • "4|Naomi|Sexy",
  • [ "ID", "Name", "Description" ]
  • );
  •  
  • // Now, let's output those values.
  • document.write( "Window Test:<br />" );
  • document.write( "ID: " + ID + "<br />" );
  • document.write( "Name: " + Name + "<br />" );
  • document.write( "Description: " + Description + "<br />" );
  • document.write( "<br />" );
  •  
  •  
  • // Now, let's test this using a custom container. Since
  • // the container is the fourth argument, we HAVE to
  • // define the delimiter as well.
  • var objTest = {};
  •  
  • SetVariables(
  • "4|Naomi|Sexy",
  • [ "ID", "Name", "Description" ],
  • "|",
  • objTest
  • );
  •  
  • // Now, let's output those values.
  • document.write( "Custom Container Test:<br />" );
  • document.write( "ID: " + objTest.ID + "<br />" );
  • document.write( "Name: " + objTest.Name + "<br />" );
  • document.write( "Description: " + objTest.Description + "<br />" );
  • document.write( "<br />" );
  •  
  • </script>
  •  
  • </body>
  • </html>

In the demo, we are testing the SetVariables() first with the default behavior and then with a custom delimiter and container. When we run this code, we get:

Splitting Strings Into Variables

Window Test:
ID: 4
Name: Naomi
Description: Sexy

Custom Container Test:
ID: 4
Name: Naomi
Description: Sexy

Notice that the variables were dynamically set and then accessed successfully in both scope environments. Remember, the trick here is to remember that a variable cannot exist on its own - it can only exist as the element of given scope.

I hope that helps.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page




Reader Comments

Dec 19, 2008 at 4:12 PM // reply »
78 Comments

Normally I think your posts are right on Ben, sometimes a little verbose, but I respect that you're trying teach people instead of just answering their question. This one might be a case of going a little overboard.

Writing a function to split out a string with two keys (based on the pipe delimiter) just comes across as excessive, and not that helpful IMO to the OP.


Dec 19, 2008 at 4:22 PM // reply »
7,572 Comments

@Andy,

Totally valid point. There was actually a second-half to this conversation which talked about converting the above functionality into a jQuery plug-in. So, first, I wanted to discuss the abstraction of the idea (above) and then probably follow it up with a post on the jQuery plug-in.

But yeah, this is a bit wordy :)


Dec 19, 2008 at 5:43 PM // reply »
26 Comments

var urlData = "foobar|http://www.myurl.com";
var urlStruct = {"label": urlData.split("|")[0], "url": urlData.split("|")[1]};


Dec 19, 2008 at 9:12 PM // reply »
78 Comments

If you assign the split to a variable first, you'll save yourself a small amount of processing, avoiding the split twice:

var urlData = "foobar|http://www.myurl.com".split("|");
var urlStruct = {"label": urlData[0], "url": urlData[1]};


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 21, 2010 at 3:42 AM
A New Wrist Pain
Chiropractic treatment is one of the best methods for treating numerous health problems naturally. After years of experience being a chiropractor, I have found that it is a powerful way to solve many ... read »
Mar 20, 2010 at 12:07 PM
Drawing On The iPhone Canvas With jQuery And ColdFusion
Simply awesome. Saved my day. ... read »
Mar 20, 2010 at 9:00 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
I would like to say thx for an easy way to create a bottom bar. I do have a ?. Is it possible to center the bar if i want to resize it to ex 85%. Regards Offenbach ... read »
Mar 19, 2010 at 7:26 PM
MySQL 3/4 - com.mysql.jdbc.Driver And allowMultiQueries=true
Thank you very much for this post. Adding allowMultiQueries="true" in context.xml didn't help until I added it to url as allowMultiQueries=true Good idea is to use prepared statements and it will he ... read »
Jim
Mar 19, 2010 at 4:49 PM
Nobody Puts Baby In The Corner!
Wow. This is like suddenly finding a support group for your secret shame. I'm not alone! I always liked this movie, even though it is extremely cheesy. I just wish Jennifer Grey hadn't gotten the ... read »
Mar 19, 2010 at 4:47 PM
Application.cfc OnRequest() Method Affects OnError() Arguments
@Jason and @Ben, I've been doing some CF9 refactoring on our systems and noticed an odd occurrence with onError as well. Found a way to work around my problem, but what I saw was... Background: Our ... read »
Jim
Mar 19, 2010 at 4:44 PM
Shoot 'Em Up Starring Clive Owen And Paul Giamatti
I actually enjoyed this movie quite a lot. It was different, certainly, but I think they were going for more of a Quentin Tarentino-"wow, that was weird"-vibe than an actual spoof. Once I realize ... read »
Mar 19, 2010 at 4:34 PM
An Intensive Exploration Of jQuery With Ben Nadel (Video Presentation)
Hey I guess the video is down. Is there anyway you can upload to youtube or vimeo or some other service? Greatly appreciated. ... read »