Right pad a string in Java
Description
The following code shows how to right pad a string.
Example
/*from ww w .j a v a 2 s . c om*/
public class Main {
public static void main(String[] argv) {
System.out.println(">" + padRight("asdf", 10) + "<");
}
public static String padRight(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 »