3

Does anyone know how I would remove all leading zeros from a string.

var str = 000890

The string value changes all the time so I need it to be able to remove all 0s before a number greater than 0. So in the example above it needs to remove the first three 0s. So the result would be 890

Joseph Stine
  • 1,022
  • 1
  • 12
  • 23
Chapsterj
  • 6,483
  • 20
  • 70
  • 124

9 Answers9

9

It looks like we each have our own ways of doing this. I've created a test on jsperf.com, but the results are showing

String(Number('000890'));

is the quickest (on google chrome).

enter image description here


Here are the numbers for the updated test based on @BenLee's comment for Firefox, IE, and Chrome.

enter image description here

JesseBuesking
  • 6,496
  • 4
  • 44
  • 89
  • For my Chrome 16 (for Linux), I am getting much closer performance for options 2 and 3 than you are (my results are regex: 5mm, classes: 17mm, parseInt: 15mm, typecasting: 22mm) – Ben Lee Dec 16 '11 at 20:37
  • 1
    Oh, I should mention I'm the one that added the 4th test for `'' + +str`, which turns out to actually be the fastest contender, outperforming the next fastest by about 50% and outperforming the slowest by a factor of 5. – Ben Lee Dec 16 '11 at 20:38
5

See: this question

var resultString = str.replace(/^[0]+/g,"");
Community
  • 1
  • 1
Anders
  • 15,227
  • 5
  • 32
  • 42
4
var resultString = str.replace(/^[0]+/g,"");
Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
2

I think a function like this should work

   function replacezeros(text){    
      var newText = text.replace(/^[0]+/g,"");
      return newText;
}
Dominic Green
  • 10,142
  • 4
  • 30
  • 34
2

If it needs to stay as a string, cast it to a number, and cast it back to a string:

var num = '000123';
num = String(Number(num));
console.log(num);

You could also use the shorthand num = ''+(+num);. Although, I find the first form to be more readable.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • +1 for simultaneously posting what I think is the cleanest-looking solution `String(Number(num))` as well as what turns out to be the fastest-peforming solution `''+(+num)` (see JesseB's answer for confirmation on that) – Ben Lee Dec 16 '11 at 20:52
2

If your problem really is as you defined it, then go with one of the regex-based answers others have posted.

If the problem is just that you have a zero-padded integer in your string and need to manipulate the integer value without the zero-padding, you can just convert it to an integer like this:

parseInt("000890", 10) # => 890

Note that the result here is the integer 890 not the string "890". Also note that the radix 10 is required here because the string starts with a zero.

Ben Lee
  • 52,489
  • 13
  • 125
  • 145
2
parseInt('00890', 10); // returns 890
// or
Number('00890'); // returns 890 
Troy Watt
  • 868
  • 8
  • 16
1

return str.replace(/^0+(.)/, '$1'));

That is: replace maximum number of leading zeros followed by any single character (which won't be a zero), with that single character. This is necessary so as not to swallow up a single "0"

smendola
  • 2,273
  • 1
  • 15
  • 14
-1

you can simply do that removing the quotation marks.

var str = 000890;
//890
var str = "000890";
//000890
Eduardo Moniz
  • 2,095
  • 3
  • 18
  • 27