2

I have a string that looks like this:

[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:

[APPLE PIE] Sei Shoujo Sentai Lakers 3 Battle Team Lakers 3 (100% FULL-PIC)

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);

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?

3
  • You want to remove this "_20121104_032834" OR "_032834" Commented Nov 26, 2012 at 11:30
  • @ameyarote: The OP states "16 digits" in the example, so it would be the former. (But the length is not fixed, as stated in the last sentence.) Commented Nov 26, 2012 at 12:17
  • @w3d: Ok..! please check the answer which I suggeted below it exactly removes all the last numbers.(Please check and tell if any diffent thing is required) Commented Nov 26, 2012 at 12:28

7 Answers 7

7

If it is always exactly 16 digits in the end of the string then:

s = s.substr(0, s.length - 16);

Otherwise you can use regexp:

s = s.replace(/[_0-9]+$/, '');
Sign up to request clarification or add additional context in comments.

Comments

2

Instead of substr, use replace and a regular expression:

str.replace(/_\d{8}_\d{6}/,'')

Running demo here.

References at MDN here.

1 Comment

Although this isn't necessarily "at the end of the string". You will need an anchor $ to mark the end of the string.
2

Using slice you can use negative indexes.

http://jsfiddle.net/gRoberts/A5UaJ/2/

var str="[APPLE PIE] Sei Shoujo Sentai Lakers 3 Battle Team Lakers 3 (100% FULL-PIC)_20121104_032834";
alert(str.slice(0, -16))​

2 Comments

But according to the OP, the length of the string to be removed varies.
Ah, apologies, I thought the OP meant the prefixed string was the variable length string. @ADC's suggestion would be better in this case.
1

just change your code to calculate the index according to the string length the following code should do the trick.

myString.substr(0, myString.length - 16); 

1 Comment

Although the OP states, "will have different amount of characters" - so it's not always 16 chars that need to be removed.
1

Here are a couple of solutions:

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

// Solution 1
// Ugly, since you don't know the number of characters
var n = str.substr(0, 75); // Take note these variables are different than yours!
document.write(n);
document.write("<br />");

// Solution 2
// Only works when the string itself contains no underscores
var n2 = str.split("_")[0];
document.write(n2);
document.write("<br />");

// Solution 3
// Only works if the last amount of numbers are always the same
var n3 = reverse(str);
n3 = n3.substr(16, n3.length);
n3 = reverse(n3);
document.write(n3);

function reverse(s){
    return s.split("").reverse().join("");
}​

JSFiddle.

Comments

0

Try this

var str="[APPLE PIE] Sei Shoujo Sentai Lakers 3 Battle Team Lakers 3 (100% FULL-PIC)_20121104_032834";
var find=str.indexOf('_');
find=find-1;
var n=str.substr(1,find);
document.write(n);

Comments

0

Check this function

Check Example - http://jsfiddle.net/QN68Q/1/

<!DOCTYPE html> 
<html> 
<body onload="check();"> 

<script>
function check()
{
var theImg= "[APPLE PIE] Sei Shoujo Sentai Lakers 3 Battle Team Lakers 3 (100% FULL-PIC)_20121104_032834";

var x = theImg.split("_");
alert(""+x[0]);
}
</script> 
</body> 
</html>

it remove last numbers , is this what you required ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.