Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trim Characters from String....Help!

 
Jennifer Johnson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to trim leading zero's from a string variable, for example: 0000006566. The number of zero's will not always be the same. Can someone help?
Thank you in advance!
Jennifer
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this for starters:
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jennifer
If the string is always going to be an int (it has no characters in it) then Integer.parseInt() will do it for you.

Or a more generic one could be

there is probably even a simpler way, but those were the first two that came to mind, hope it helps.
 
Ravi Veera
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a fancy approach if you want to look cool and show off your knowledge of JDK1.4

String stest="000000506505";
while (stest.startsWith("0",0))
{
stest=stest.replaceFirst("0","");
stest=stest.trim();
}
System.out.println("test="+stest);
 
Jennifer Johnson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you to everyone for replying! You've all helped me so much!
Jennifer
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic