Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Left Padding a String with ZERO

843789Nov 21 2009 — edited Nov 22 2009
Hi All,

I have to left-pad a string with zeroes. I tried using String.format but it gives me an error. I have looked at a similar thread [here |http://forums.sun.com/thread.jspa?forumID=54&threadID=5374247] but that deals with integer type and not string type.

I think the error I am getting is because String.format tries to add ZEROes to an integer type and not String. I cannot do a Integer.parseInt(myString) as myString is in Hexadecimal notation. Please help me figure this one out.

In a nutshell:
My existing Hex string: ffe
The string I require: 00000ffe
Here is my code:
public String padLeft (String myString, int n)
{
myString=String.format("%0"+n+"x",myString);
return myString;

}
What am I doing incorrectly?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 20 2009
Added on Nov 21 2009
6 comments
1,947 views