Left pad a string in Java
Description
The following code shows how to left pad a string.
Example
/*from w ww .ja va 2 s . c o m*/
public class Main {
public static void main(String[] argv) {
System.out.println(">" + padLeft("asdf", 10) + "<");
}
public static String padLeft(String s, int n) {
return String.format("%1$#" + n + "s", s);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »