SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Apr 2002
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problems with substring....

    hi,

    I would appreciate if anyone tells me, what the h... am I doing wrong....


    here is the foll crapy code.....

    var1 = stringvar.substr(0,4)
    var2 = stringvar.substr(5,2)
    var3 = stringvar.substr(8,2)

    for values 2002-04-11 it works well, I mean

    var1 gets 2002, var2 gets 4, and var3 gets 11

    but for a value....

    2002-04-08 it does the following....

    var1 gets 2002, var2 gets 4, and var3 gets 0

    if I print the stringvar.substr(0) I get the full value 2002-04-08.

    thanks,
    esv.
    Last edited by esanchezvela; Apr 15, 2002 at 17:05.

  2. #2
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it does work?


    PHP Code:
    var stringvar = new String('2002-04-01');

    var1 stringvar.substr(0,4
    alert(var1//2002
    var2 stringvar.substr(5,2)
    alert(var2//04
    var3 stringvar.substr(8,2)
    alert(var3//01


    var stringvar = new String('2000-02-11');

    var1 stringvar.substr(0,4
    alert(var1//2000
    var2 stringvar.substr(5,2)
    alert(var2//02
    var3 stringvar.substr(8,2)
    alert(var3//11 
    Last edited by Andrew-J2000; Apr 15, 2002 at 14:21.

  3. #3
    SitePoint Enthusiast Incubator's Avatar
    Join Date
    Apr 2002
    Location
    Good old Belgium
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, Ive been playing around with substrings (thx to Andrew) and indeed the code you gave works.
    I also tried the above code from Andrew, and it works fine too
    You were right after all, esanchezvela
    "Press any key to continue. Press any other key to exit."

  4. #4
    SitePoint Member
    Join Date
    Apr 2002
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    thanks, but my code does not, at least on my browser NS6 4 linux. (not blaming anyone).

    let me explain first, the value of the first string comes in the form of a function parameter using the value of a textfield....ie...


    <input .... blah blah... onClick=" weirdfunctionname(this.form.mytextfield.value);" >


    on a javascript file (myfunctions.js) there is a


    function weirdfunctionname( stringvalue) {
    var1 = stringvalue.substr(0,3)
    var2 = stringvalue.substr(5,2)
    var3 = stringvalue.substr(8,2)
    alert('var1-var2-var3 = '+var1+'-'+var2+'-'+var3);
    }


    probably I should use the toString method. will try it tonight.!

    thanks,
    esv.

  5. #5
    SitePoint Enthusiast Incubator's Avatar
    Join Date
    Apr 2002
    Location
    Good old Belgium
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, i may have something that *might* help. It is in dutch though, but take a look at the JS code and try it out
    Code:
    <html>
    <head>
    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin script
    function LeesActeur() 
      {
        var strNaam
        strNaam=prompt("hoe heet je? ","Tik hier je naam");
        if (strNaam=="Tik hier je naam" || strNaam==null || strNaam=="") 
          strNaam="mr-X";
        return (strNaam);
      }
    -->
    </SCRIPT>
    </head>
    <body>
    <script language="JavaScript">
    <!--
    document.lastModified
    document.write("Hallo ", LeesActeur()+"!");
    -->
    </script>
    <marquee behavior=alternate><table border=1 bgcolor="red"><tr><th>Hier is uw samenvatting van linux :)</th></tr></table></marquee>
    </body>
    </html>
    what im trying to say is: you could use the prompt thing to insert the date into the variable.
    "Press any key to continue. Press any other key to exit."

  6. #6
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sry, I cant help with browser compatibility as i cannot stand netscape, so i basically design for ie.
    But I will have ago at another similar method

  7. #7
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mess about with this with the values

    i'm sure this will work properly, i just dont have time to do it properly for you now.

    PHP Code:

    var stringvar = new String('2000-02-11');

    var1 stringvar.substring(0,4
    alert(var1
    var2 stringvar.substring(6,1)
    alert(var2
    var3 stringvar.substring(8,2)
    alert(var3

    notice it's subtring instead of substr

  8. #8
    SitePoint Member
    Join Date
    Apr 2002
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Andrew,

    I hate to say it, I alredy tried that before, I will try a different browser either staroffice (hope it works), kde one or something else.

    thanks,
    esv.

  9. #9
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what is your actual problem though, is it because the string dont align, or just dont work full stop?

  10. #10
    SitePoint Member
    Join Date
    Apr 2002
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my fault....sorry folks.....

    I had this stupid call to parseInteger() without radix, the default for values starting with 0 is octal, returning 0 for a 08 value.


    thanks for your time,
    esv.

  11. #11
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    np, at least it's working i hope


Bookmarks

Posting Permissions

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