SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2002
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I have a question about strings

    How do you get rid of the last letter of a string? I know how to get the last letter:

    the_string.charat(the_string.length-1);

    But how do you get rid of that letter after you get it?

  2. #2
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm...thinking...ok got it.

    Code:
    <script language="javascript">
    <!--	
    	var hello="bobbby";
    	var test=new String(hello);
    	var len=test.length-1;
    	document.write(test.substring(0,len));		
    //-->
    </script>
    To quickyl explain how this works define the variable first (I like dumb names ). Feed it into an object, since it is a string call it that. Find the length and minus the number of characters you don't want to show. The substring it. To define this

    substring(start,number of character)

    remember 0 is the first number in javascript.
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  3. #3
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    joekarbakuitz, the thing is not to remove the character you don't want, but to grab all the ones you do want (and leave the one you don't)! That's what Maelstrom's code does.

    If you prefer it as a function:
    Code:
    function StripLastChar(str)
    {
    	return(str.substring(0,str.length-1));
    }

    M@rco

  4. #4
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by M@rco
    joekarbakuitz, the thing is not to remove the character you don't want, but to grab all the ones you do want (and leave the one you don't)! That's what Maelstrom's code does.

    If you prefer it as a function:
    Code:
    function StripLastChar(str)
    {
    	return(str.substring(0,str.length-1));
    }

    M@rco
    No I do think ours do the same thing. I just left it spread out for ease of use. Like your would look like this

    str="bob"
    cut=str.length-1
    newString=str.substring(0,cut)

    Same thing just elongated to show all the steps.
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  5. #5
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maelstrom,

    I'm well aware that ours are identical!!

    I was merely expanding on your explanation, then providing a (neater) version of the code as a function. (I'm in an efficient code mood today)


    Regards,

    M@rco

  6. #6
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by M@rco
    Maelstrom,

    I'm well aware that ours are identical!!

    I was merely expanding on your explanation, then providing a (neater) version of the code as a function. (I'm in an efficient code mood today)


    Regards,

    M@rco
    Ahhh NM than I misunderstood. But yes yours is by far cleaner and more efficient. I guess I was in one of my everything need to be fully explained moods. (Kind of like grade 9 math having to show all of your work haha )
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2002
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Geez, lots of answers. I figured it out like two seconds I posted the question ironicly. Your ideas were easier and better than the one I had though. I did this:

    var string="the string";
    var string_array=string.split("");
    string="";
    for (i=0; i<string_array.length-1; i++) {
    string+=string_array[i];
    }

    Check out this neato thing I doing and tell me if its ok and what it need (there is still a lot of bugs but it is coming along) at:

    http://www.html4me.com/ryant/writingdocument.html

    thanks.

  8. #8
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maelstrom,
    LOL!


    Joekarbakuitz,
    It looks very promising - an original spin on the standard MSHTML editor. May I suggest that you add the UNSELECTABLE attribute to the contents of the About (and other) layers so that you cannot select text when you click on it. I also had a VERY weird problem with the cursor staying where it is no matter what text was selected or where in the document I was typing!!!

  9. #9
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cool scripting!

  10. #10
    SitePoint Enthusiast
    Join Date
    Feb 2002
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    marco- I never enabled the cursor to move with the yet. I was having problems with it so I just left it alone while I was working on other parts of the page.

    I didnt event know that there was an unselectable attribute, ill look into that.

    I added my very own scrollbar-completly original-that doesnt work very well but is pretty nifty.

    Could you tell me anything that it needs? That would be helpful. thx.


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
  •