#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    8
    Rep Power
    0

    Remove the last 16 digits at the end of text


    I have a string that looks like this:
    Code:
    [APPLE PIE] Sei Shoujo Sentai Lakers 3 Battle Team Lakers 3 (100% FULL-PIC)_20121104_032834
    I want to remove the digits at the end of the string, basically the 16 digits at the end of the string. In the end it should look like this:
    Code:
    [APPLE PIE] Sei Shoujo Sentai Lakers 3 Battle Team Lakers 3 (100% FULL-PIC)
    This is my code that I have written so far
    Code:
    var str="[APPLE PIE] Sei Shoujo Sentai Lakers 3 Battle Team Lakers 3 (100% FULL-PIC)_20121104_032834";
    var n=str.substr(1,74);
    document.write(n);
    The problem is the string will be different so each will have different amount of characters. So how I remove the digits at the end of the string in javascript?
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    use slice() with a negative end index:

    Code:
    var extr = str.slice(0, -16);
    A negative index will count from the end of the string, beginning with -1. Be careful with the start index: It begins with 0, so when you use 1, you'll lose the first character.

    Another possibility would be to simply take the string length and substract 16:

    Code:
    var extr = str.substr(0, str.length - 16);

    Comments on this post

    • Kravvitz agrees

IMN logo majestic logo threadwatch logo seochat tools logo