How to remove leading zeros from alphanumeric text?
How to remove leading zeros from alphanumeric text?
https://stackoverflow.com/questions/2800739/how-to-remove-leading-zeros-from-alphanumeric-text
I've seen questions on how to prefix zeros here in SO. But not the other way!
Can you guys suggest me how to remove the leading zeros in alphanumeric text? Are there any built-in APIs or do I need to write a method to trim the leading zeros?
Example:
01234 converts to 1234
0001234a converts to 1234a
001234-a converts to 1234-a
101234 remains as 101234
2509398 remains as 2509398
123z remains as 123z
000002829839 converts to 2829839
Regex is the best tool for the job; what it should be depends on the problem specification. The following removes leading zeroes, but leaves one if necessary (i.e. it wouldn't just turn "0"
to a blank string).
s.replaceFirst("^0+(?!$)", "")
The ^
anchor will make sure that the 0+
being matched is at the beginning of the input. The (?!$)
negative lookahead ensures that not the entire string will be matched.
Test harness:
String[] in = {
"01234", // "[1234]"
"0001234a", // "[1234a]"
"101234", // "[101234]"
"000002829839", // "[2829839]"
"0", // "[0]"
"0000000", // "[0]"
"0000009", // "[9]"
"000000z", // "[z]"
"000000.z", // "[.z]"
};
for (String s : in) {
System.out.println("[" + s.replaceFirst("^0+(?!$)", "") + "]");
}
See also
-
32
-
4@Greg: This question is about Java, not JavaScript. Java SE has had the method String.replaceFirst() since version 1.4.– JonikMay 22, 2012 at 10:37
-
7adding trim() to s.replaceFirst("^0+(?!$)", "") (ie. s.trim().replaceFirst("^0+(?!$)", "") will help in removing padded spaces!– AVAMar 12, 2014 at 11:05
-
2isn't regex a bit expensive for such a simple task?=============https://www.tutorialspoint.com/remove-leading-zeroes-from-a-string-in-java-using-regular-expressions
The replaceAll() method of the String class accepts two strings representing a regular expression and a replacement String and replaces the matched values with given String.
Following is the regular expression to match the leading zeros of a string −
The ^0+(?!$)";
To remove the leading zeros from a string pass this as first parameter and “” as second parameter.
Example
The following Java program reads an integer value from the user into a String and removes the leading zeroes from it using the Regular expressions.
import java.util.Scanner; public class LeadingZeroesRE { public static String removeLeadingZeroes(String str) { String strPattern = "^0+(?!$)"; str = str.replaceAll(strPattern, ""); return str; } public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter an integer: "); String num = sc.next(); String result = LeadingZeroesRE.removeLeadingZeroes(num); System.out.println(result); } }
Output
Enter an integer: 000012336000 12336000
=====================================
https://www.geeksforgeeks.org/remove-leading-zeros-from-a-number-given-as-a-string/Time Complexity: O(N)
Auxiliary Space: O(N)Space-Efficient Approach:
Follow the steps below to solve the problem in constant space using Regular Expression:- Create a Regular Expression as given below to remove the leading zeros
regex = “^0+(?!$)”
where:
^0+ match one or more zeros from the beginning of the string.
(?!$) is a negative look-ahead expression, where “$” means the end of the string.- Use the inbuilt replaceAll() method of the String class which accepts two parameters, a Regular Expression, and a Replacement String.
- To remove the leading zeros, pass a Regex as the first parameter and empty string as the second parameter.
- This method replaces the matched value with the given string.
Below is the implementation of the above approach:
- C++
- Java
- Python3
- C#
- Javascript
// Java Program to implement
// the above approach
import
java.util.regex.*;
class
GFG
{
// Function to remove all leading
// zeros from a a given string
public
static
void
removeLeadingZeros(String str)
{
// Regex to remove leading
// zeros from a string
String regex =
"^0+(?!$)"
;
// Replaces the matched
// value with given string
str = str.replaceAll(regex,
""
);
System.out.println(str);
}
// Driver Code
public
static
void
main(String args[])
{
String str =
"0001234"
;
removeLeadingZeros(str);
}
}