CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2002
    Posts
    90

    Need an example of the Split command in JavaScript

    I need a clear explanation and example of the use of the Split method in a JavaScript function.

  2. #2
    Join Date
    Feb 2003
    Location
    Oakland, CA
    Posts
    29
    It's very similar to a StringTokenizer in java, at least in terms of purpose and effect.

    It is an inherent method available on any variable that is of type String. You pass a string to it and get an array of strings that are composed of all the pieces of the original string that were split up at the point where the argument was found.

    So, if you have a string like this:

    var myString = "a,b,c,d,e,f-g,h,i,j,k";
    var splits = myString.split("-");

    splits will be an array with two elements, "a,b,c,d,e,f" and "g,h,i,j,k";

    Now you could go further if you like with :

    var partOne = splits[0].split(",");
    var partTwo = splits[1].split(",");

    partOne will be an array with 6 elements
    partOne[0] = "a"
    partOne[1] = "b"
    partOne[2] = "c"
    partOne[3] = "d"
    partOne[4] = "e"
    partOne[5] = "f"

    You can probably guess what partTwo would be.

    Hope this helps.
    Nate Grover
    http://www.nategrover.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured