www.webdeveloper.com
Results 1 to 14 of 14

Thread: Parsing the string in JavaScript

  1. #1
    Join Date
    Apr 2005
    Posts
    21

    Parsing the string in JavaScript

    Hi all,

    I have an array of attributes where each of them have a number of numbers(for example: "M50 100 V10 h50 z" is the attribute for one of them).

    I have to parse these string so that I can get the second value, for example the numer 100 in the above example code. Is there any way that I can parse the strings?

    Thanks in advance.

  2. #2
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    var txt = "M50 100 V10 h50 z";

    var num = txt.split(" ")[1];
    alert(num);
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  3. #3
    Join Date
    Apr 2005
    Posts
    21
    Thanks, scragar. For the above, it returns "5" for var num = txt.split("")[1], which means it split one character by one character instead of spliting them up from the spacing.

  4. #4
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    there is a space between the two quotes that are in brackets.
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  5. #5
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    If you can't make it work you could try:
    Code:
    var num = txt.split(/\s+/)[1]
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  6. #6
    Join Date
    Apr 2005
    Posts
    21
    opppss.... i got it. I am so careless. I miss out the " " space between the two aprostrophe. Thanks a lot, scragar!

  7. #7
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    how annoying that the simplest thing is often the hardest to find.
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  8. #8
    Join Date
    Apr 2005
    Posts
    21
    ... and this is the thing that always happen....
    Btw, I have another enquiry:

    I have a list of numbers now, which I extracted from the attributes (e.g. all the second value for the splited string). I need to get the smallest number among them. So what i did is keep those splited numbers in an array and then arrange them in a numeric sort (smallest to largest, for example, I have 350,120, 400, 100,
    200). Then will get the smallest number among them.

    What I did here is

    var count=0;
    function parseFile(xmlDOM){
    if (path[z].getAttribute('class') == 'outline'){
    var dOt = path[z].getAttribute('d');
    var num = dOt.split(" ")[1];
    var arrayOt = new Array(count);
    arrayOt[count-1] = num;
    alert("Y for the outline is " + arrayOt[count-1]);
    compareNum();
    }
    alert("sorted: " + arrayOt.sort(compareNum));
    }

    function compareNum(a,b) {
    return a-b
    }

    But the outcome is : ,,,,200;
    How come....????

    Thanks in advance!

  9. #9
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    by asigning a new array your clearing values!

    PHP Code:
    var arrayOt = new Array();
    function 
    parseFile(xmlDOM){
    if (
    path[z].getAttribute('class') == 'outline'){
    var 
    dOt path[z].getAttribute('d');
    var 
    num dOt.split(" ")[1];
    /*
    if your using numbers only then you might want to replace:
    var num = dOt.split(" ")[1];
    with
    var num = parseInt(dOt.split(" ")[1]);
    */
    arrayOt[arrayOt.length] = num;//fixed here.
    alert("Y for the outline is " arrayOt[arrayOt.length-1]);
    compareNum();
    }
    alert("sorted: " arrayOt.sort(compareNum));
    }

    function 
    compareNum(a,b) {
    return 
    a-b

    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  10. #10
    Join Date
    Apr 2005
    Posts
    21
    It works!!! Thanks a lot scragar! You are star!!!

  11. #11
    Join Date
    Apr 2005
    Posts
    21
    A small question again.....
    As I already have the numerical sort of numbers and i want the smallest number among them, then am i correct to write in this way?

    ....
    alert("sorted: " + arrayGL.sort(compareNum));
    var smallGL = arrayGL.sort(compareNum);
    var smallGLy = smallGL.split(",")[0];
    alert("The smallest y of all the guideLlines class is " + smallGLy);

    There are two ways I can get the smallest number, either from the array by comparing those numbers or again, to split those numbers with "," (as above). Which one you reckon is better for programming?

  12. #12
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    PHP Code:
    var arrayOt = new Array(); 
    function 
    parseFile(xmlDOM){ 
    if (
    path[z].getAttribute('class') == 'outline'){ 
    var 
    dOt path[z].getAttribute('d'); 
    var 
    num parseInt(dOt.split(" ")[1]); 
    arrayOt[arrayOt.length] = num;
    alert("Y for the outline is " arrayOt[arrayOt.length-1]); 
    compareNum(); 

    var 
    arrayGL arrayOt.sort();
    alert("sorted: " arrayGL);

      
    alert("the smallest number is "+arrayGL[0]);
      
    //   arrayGL is still an array, so you still use the square brackets
      //   without spliting it although it alerts the comma's.



    function 
    compareNum(a,b) { 
    return 
    a-

    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  13. #13
    Join Date
    Apr 2005
    Posts
    21
    Thanks again, scragar!!

  14. #14
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    your welcome.
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles