Avatar of MehtaJasmin
MehtaJasmin
Flag 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?
Java

Avatar of undefined
Last Comment
n2fc

8/22/2022 - Mon