Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    46
    Thanks
    9
    Thanked 2 Times in 2 Posts

    Split function creating a random comma

    Hi there!

    I'm trying to retrieve the integer value in the string: #10
    So I'm using the JS Split function

    Code:
    <script type="text/javascript">
    
    var str="#10";
    document.write(str.split("#"));
    
    </script>
    Is the code I'm using, but I keep getting a comma before my result..
    in this case, my result is ",10"

    What should I do?
    Thanks!

    Kind Regards
    Matthew

    P.S. I know that it's an array, but is there still anyway for me to get rid of the comma?
    Last edited by GreenFanta; 08-18-2011 at 11:34 AM.

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    document.write(str.split("#")[1]);

  • Users who have thanked DaveyErwin for this post:

    GreenFanta (08-18-2011)

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    46
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Ah, thank you, I don't know why I didn't think of that...

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Another way to strip the first character is

    Code:
    var str="#10";
    str = str.substring(1);
    document.write(str);
    Or convert to a number using

    Code:
    str = Number(str.substring(1));

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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