Separte a sting and put int variables

Posted on 2006-04-12
Last Modified: 2010-08-05
I have the following function:

<script language=JavaScript>
  ctrl =;

The "ctrl" value becomes "Equip&Descr=Descr"

I need to separate the string that´s in the "crl" into 2 more variables. I need that it reads "Equip" and put´s it in a variable and then reads "Descr" and put´s it in anther variable. How can I do this ???
Question by:fabyola
    LVL 17

    Accepted Solution

    Hi fabyola,

    var var1 = ctrl.substring(0, ctrl.indexof("&"));  // <-- should be "Equip"
    var var2 = ctrl.substring(ctrl.indexof("=")+1);  // <-- should be "Descr"

    Joe P
    LVL 11

    Expert Comment

    Hi fabyola,

    Here's another way.  Say you have:

    var ctrl = "Equip&Descr=Description";

    You can do this:

    var v1 = ctrl.split("=");

    Now, v1[0]="Equip&Descr", and v1[1]="Description"

    Then, you can do this:

    var v2 = v1[0].split("&");

    Of course, now v2[0]="Equip" and v2[1]="Descr" .

    Put it all together and here it is:

    var s1=ctrl.split("&")[0];  // <-- should be "Equip"
    var s2=ctrl.split("&")[1].split("=")[0];  // <-- should be "Descr"
    var s3=ctrl.split("=")[1];  // <-- should be "Description"

    Peace and joy.  mvan
    LVL 11

    Expert Comment

    Hehe, here's a nasty one:


    var ctrl = "Equip&Descr=Description";

    //  Next line creates variable named Equip, and sets it = 'Description' !!
    eval("var "+ctrl.split("&")[0]+"='"+ctrl.split("=")[1]+"'");
    alert("Equip = "+Equip);

    //  Next line creates variable named Descr, and sets it = 'Description' !!
    eval("var "+ctrl.split("=")[0].split("&")[1]+"='"+ctrl.split("=")[1]+"'");
    alert("Descr = "+Descr);


    Pro'ly not good programming practice.  ;-)

    Peace and joy.  mvan
    LVL 48

    Expert Comment

    by:Ryan Chong
    or you can try this function:

    function getParameter ( queryString, parameterName ) {
          //Use like UniqueID = getParameter ( , "uid" );

          // Add "=" to the parameter name (i.e. parameterName=value)
          var parameterName = parameterName + "=";
          if ( queryString.length > 0 ) {
                // Find the beginning of the string
                begin = queryString.indexOf ( parameterName );
                // If the parameter name is not found, skip it, otherwise return the value
                if ( begin != -1 ) {
                      // Add the length (integer) to the beginning
                      begin += parameterName.length;
                      // Multiple parameters are separated by the "&" sign
                      end = queryString.indexOf ( "&" , begin );
                      if ( end == -1 ) {
                            end = queryString.length;
                      // Return the string
                      return unescape ( queryString.substring ( begin, end ) );
                // Return "null" if no parameter has been found
                return "null";

    use it like:

    alert("Equip = "+getParameter(, "Equip"));
    alert("Descr = "+getParameter(, "Descr"));

    hope this helps too, cheers

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
    In my daily work (mainly using, I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now