1

I have a string COR000001. I want to split it so that I get only the integer 1. If the String is like COR000555 I should get the integer 555. Thank you...

4
  • so you basically want all integers after 0 Commented Jul 19, 2012 at 5:20
  • Does your structure contains only COR*****? Commented Jul 19, 2012 at 5:22
  • no, there will be other like MCR,TCP,VCF... Commented Jul 19, 2012 at 5:26
  • if only 3 letters then you need refp's answer Commented Jul 19, 2012 at 5:30

2 Answers 2

4

The easiest method to use is to get rid of the first three characters "COR", "MCR", "TCP", etc.. and then use parseInt with the appropriate parameters such as in the below.

var str = "COR000555";
var n   = parseInt (str.substr (3), 10); // force parseInt to treat every
                                         // given number as base10 (decimal)

console.log (n);

555

If the "key" in the beginning is not always limited to three characters you could use a regular-expression to get all the digits in the end of your string.

.. as in the below;

var n = parseInt (str.match (/\d+$/)[0], 10);
Sign up to request clarification or add additional context in comments.

6 Comments

what if he has CORE instead of COR
@AnkitGautam I'm about to write an edit answering just that, hold on.. but according to the post it seems as if it's always three characters. but as said, hold on.
Hey that is exactly what I am looking for. There will be no "CORE" all of the combination will have only 3 letters. Thanks refp :)
Can you tell me why we use console.log(n) here?
console.log() shows the result in the browser's console, like google chrome's inspect element or firebug
|
0

I had just seen some one answer this question and before i could up vote it was deleted, hence posting the solution on his behalf.

var str='COR000050ABC';
var variable=parseFloat(/[0-9]+/g.exec(str));

though there was a small modification, added parseFloat

1 Comment

this doesn't follow the description posted by OP, also; why would you use parseFloat when parseInt will be sufficient - and OP requested an integer?

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.