Link to home
Create AccountLog in
Avatar of NEX_Developer
NEX_DeveloperFlag for United States of America

asked on

How to remove leading 0s from String

I have some card number that gets extracted from the record as a fixed size string. The string has always all numbers.

Like for example, my record is:

PN02210220063980223161335590100000000000001234000301234000300000000000000000000000000000000000900

and I always extract the string from between the  fixed positions. That gives me "00001234000301234"

What is the best API to use to trim the leading zeros and get only "1234000301234". The number of 0s in leading can very in counts anywhere from none to 6.

I see some posting
s.replaceFirst("^0+(?!$)", "")

Open in new window


or
String card = "00001234000301234";// 
card = Integer.valueOf(card).toString();
System.out.println(card);

Open in new window


But what is the best approach?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of NEX_Developer

ASKER

Thank You. That meets my requirement.
:)
    String card = "00001234000301234"; 
    for (int i=0; i<card.length(); i++) {
       while (card.substring(i, i+1).equals("0")) i++;  
       card = card.substring(i); break; 
    }
    System.out.println(card);

Open in new window