Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove first character from a string if it is 0

I know it is related to Remove first character from a string if it is a comma but in this case that solution doesn't work for me. Maybe its because now I'm dealing with a number. Simply I want javascript to remove 0 from 08 but don't touch 10; only remove 0 when it is the first character.

This solution doesn't work: replace(/^0/, "")

like image 607
newzad Avatar asked Nov 05 '15 12:11

newzad


People also ask

How to remove the first character of a string in JavaScript?

You can remove the first character of a string using substring: var s1 = "foobar"; var s2 = s1.substring (1); alert (s2); // shows "oobar" var s = "0000test"; while (s.charAt (0) === '0') { s = s.substring (1); }

How do I remove a character from a string in Python?

The deleteCharAt () method accepts a parameter as an index of the character you want to remove. Remove last character of a string using sb.deleteCharAt (str.length () – 1). Remove first character of a string using sb.deleteCharAt (0). Now, print the modified string.

How to remove characters from a string using algorithm?

Algorithm: Let the first input string be a ”test string” and the string which has characters to be removed from the first string be a “mask” Construct count array from mask_str. The count array would be:

How to remove leading zeros from numbers that start in 0?

Some start in 0 and some don't. For the ones that start in 0 I would like to remove the 0. Find and replace wouldnt work as some of the numbers have a 0 at other positions in the number. How can I removed the first character if it is an 0? =A1+0 will remove leading zeros, if the cell holding the formula is formatted General.


1 Answers

You can try this:

alert("08".replace(/^0+/, ''));

DEMO

And if you are dealing with numbers then you can use parseInt() function.

like image 57
Rahul Tripathi Avatar answered Sep 28 '22 13:09

Rahul Tripathi