sioesi
sioesi

Reputation: 517

Delete char from String

I have a string like this: 0000323530, I need to eliminate all the 0 on the left until it reaches number 3 (for example, it could be another number ALWAYS different from 0)

Upvotes: 1

Views: 52

Answers (2)

Jordan Soltman
Jordan Soltman

Reputation: 3903

Just replace the beginning zeros with nothing using regex.

var num = "0000323530"
console.log(num.replace(/^0+/, ""))

Upvotes: 3

Titus
Titus

Reputation: 22484

You can use regex for that, here is an example:

function removeLeadingZeros(str){
    return str.replace(/^0+/, "")
}

console.log(removeLeadingZeros("0000323530"));

^ stands for the start of the string

0 stands for the character 0

+ stands for one or multiple of the previous char or group

which means that this regex will match all the zeros at the start of the string.

Upvotes: 3

Related Questions