
how to add spaces with string functions.?

Hi Friend,
Try the following code:
class AddSpacesToString
{
public static String addSpaceToRight(String s, int n) {
return String.format("%1$-" + n + "s", s);
}
public static String addSpaceToLeft(String s, int n) {
return String.format("%1$#" + n + "s", s);
}
public static void main(String args[]) throws Exception {
System.out.println(addSpaceToRight("Hello", 10) + "$");
System.out.println(addSpaceToLeft("Hello", 15) + "$");
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.