How to strip off zero's from the front of a String

Hi I have an eight char customer ID, it will always be eight characters long

examples
'00054398'
'89700937'
'02329329'

Now what I need is a method which gives a number without the zeroes in the front if I pass the number to that method.

'54398'
'89700937'
'2329329'
Any help will be greatly appreciated
royalcyberAsked:
Who is Participating?
 
radarshConnect With a Mentor Commented:
Another solution.

String input = "0003483";
String output = input.replaceAll("^0*", "");

________
radarsh
0
 
appxpeteCommented:
public static String removeLeadingZeros(String str)
         {
                 if (str == null)
                 {
                         return null;
                 }
                 char[] chars = str.toCharArray();
                 int index = 0;
                 for (; index < str.length(); index++)
                 {
                         if (chars[index] != '0')
                         {
                                 break;
                         }
                 }
                 return (index == 0) ? str : str.substring(index);
         }
0
 
mukundha_expertCommented:
You can also do it this way,

StringBuffer buf = new StringBuffer ( inputString ) ;
while ( buf.charAt( 0 ) == '0') buf.deleteCharAt ( 0 );

String outString = buf.toString () ;

Cheers,
Mukundh
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
radarshCommented:
Hi royalcyber,

Try this:

String input = "0003483";
String output = String.valueOf(Long.parseLong(input));

________
radarsh
0
 
hoomanvCommented:
nice jobs
appxpete's solution is fast
radarsh's is short

and here is mine

replaceFirst("^0*", "");

I think its faster than replaceAll since it discontinues sooner. is it ?
0
 
radarshCommented:
Yes it is notably faster than replaceAll.

________
radarsh
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.