Remove the last 16 digits at the end of text

Discussion in 'JavaScript' started by Ruriko, Nov 26, 2012.

  1. #1
    I have a string that looks like this:
    [APPLE PIE] Sei Shoujo Sentai Lakers 3 Battle Team Lakers 3 (100% FULL-PIC)_20121104_032834
    Code (markup):
    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:
    [APPLE PIE] Sei Shoujo Sentai Lakers 3 Battle Team Lakers 3 (100% FULL-PIC)
    Code (markup):
    This is my code that I have written so far
    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);
    Code (markup):
    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?
     
    Ruriko, Nov 26, 2012 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    Not sure if this is the cleanest way to do it but for me it was the quickest.

    It basically removes any numbers and underscores from the end of the string
    
    <script type="text/javascript">
    
    var str="[APPLE PIE] Sei Shoujo Sentai Lakers 3 Battle Team Lakers 3 (100% FULL-PIC)_20121104_032834";
    
    var allowed = ["0","1","2","3","4","5","6","7","8","9","_"];
    var n = "eeee";
    for(var c=str.length-1; c>0;c--) {
        var let = str.substr(c,1);
        if (allowed.indexOf(let) === -1 ) {
            n = str.substr(0,(c+1));
            c=0;
        }
    }
    
    document.write(n);
    
    </script>
    
    Code (markup):
     
    stephan2307, Nov 26, 2012 IP
  3. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #3
    If there are no _ in the first part that you want to keep then you could do this

    
    var str="[APPLE PIE] Sei Shoujo Sentai Lakers 3 Battle Team Lakers 3 (100% FULL-PIC)_20121104_032834";
    var parts=str.split("_");
    var n = parts[0];
    document.write(n);
    
    Code (markup):
    but like I said if there is a _ inside the text you want to keep then it would break

    if the initial str would be

    var str="[APPLE PIE] Sei Shoujo Sentai Lakers_ 3 Battle Team Lakers 3 (100% FULL-PIC)_20121104_032834";

    then the output would be

    [APPLE PIE] Sei Shoujo Sentai Lakers

    So not sure if this is a valid solution. But certainly a lot easier and quicker.
     
    stephan2307, Nov 26, 2012 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    Wow, you guys are doing this the hard way... if it's always 16 characters to be removed, subtract 16 from it's length for substring.

    str=str.substring(0,str.length-16);

    That's it, simple.

    Though I would give some serious thought to using lastIndexOf twice if the format always uses underscores, just in case the number of digits to remove is not constant.

    removeCount=str.lastIndexOf('_');
    id=str.substring(removeCount+1);
    str=str.substring(0,removeCount);
    removeCount=str.lastIndexOf('_');
    date=str.substring(removeCount+1);
    str=str.substring(0,removeCount);

    Also pulling those other values (I see one of them is a date, that could be important) for use if you want them.
     
    deathshadow, Nov 26, 2012 IP