SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2002
    Location
    Asker, Norway
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    get the 3 rightmost charachters from a string

    I need to get the 3 rightmost charachters from a string, like the vb function Right(string,3) but I don't know how to do this in javascript.

  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)
    Quote Originally Posted by claus erik
    I need to get the 3 rightmost charachters from a string, like the vb function Right(string,3) but I don't know how to do this in javascript.
    Code:
      function right(str, chr)
      {
    	return str.slice(myString.length-chr,myString.length);
      }
      function left(str, chr)
      {
    	return str.slice(0, chr - myString.length);
      }
      myString = new String('I need to get the 3 rightmost charachters from a string, like the vb function Right(string,3) but I don\'t know how to do this in javascript.');
      alert( right(myString, 12) );
      alert( left(myString, 6) );
    this will return "javascript." & "I need", hopeflly these function will be a bit more usefull to you.

  3. #3
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could also use substr like this:
    <script language="JavaScript">
    function right(str,chr)
    {
    return newstr=str.substr(str.length-chr,str.length)
    }
    function left(str,chr)
    {
    return newstr=str.substr(0,chr)
    }
    var mystring="this is a string"
    alert(right(mystring,3));
    alert(left(mystring,3));
    </script>
    Last edited by requestcode; Feb 26, 2003 at 07:59.

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Andrew, there's better ways to manipulate slice()
    Code:
    function right(str, chr)
    {
    return str.slice( -( chr ) );
    }
    function left(str, chr)
    {
    return str.slice( 0, chr );
    }
    Of course, if I was applying these, I'd make methods...
    Code:
    String.prototype.right = function( qty )
    {
    return this.slice( -( qty ) );
    }
    String.prototype.left = function( qty )
    {
    return this.slice( 0, qty );
    }
    Of course, I'm not a big fan of any function/method that just allows a slightly different interface to existing methods. I'd just stick with using slice()
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    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)
    I agree, but for simplicity I just wrapped it into a small function, so it is actually like the left() & right() vb function, which claus erik seems to be more familiar with.


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
  •