Inserting Spaces
This is probably a simple question, but is there a method that returns a String consisting of a designated number of spaces? In other languages you can do something like Space(10) to get a string of 10 spaces, but I don't seem to find an equivalent in Java yet, and I'm not sure what to search for or what API to read.More specifically, I need to pad an existing string with trailing spaces. I've used a PadR( String, int ) method in another language, but again I'm lost with Java.
My only solution is to make a loop that adds a single space each time until a certain number is met, but I know there has to be a more efficient answer. Please help.
Look at the String.format method (Assuming you are using 1.5 or later) http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#format(java.lang.String,%20java.lang.Object...)~Tim
Yes I looked at that, but it doesn't really do what I want. Basically, I'm formatting a series of lines so that the columns line up properly. To do this, I need to measure a string, then apply a number of spaces equal to the length minus the total length I want for the string to be. So if the string is 6 characters, I want to add 4 spaces. If it is 8 characters, add 2 spaces, and so on.
Doesn't do what you want, eh?
public class FormatExample {
public static void main(String[] args) {
String[][] data = {
{"1","999999999"},
{"22","88888888"},
{"333","7777777"},
{"4444","666666"},
{"55555","55555"},
{"666666","4444"},
{"7777777","333"},
{"88888888","22"},
{"999999999","1"},
};
for(String[] row : data) {
System.out.format("|%-10s|%10s|%n", row[0], row[1]);
}
}
}
> Yes I looked at that, but it doesn't really do what I want. Yes, it does. What you want is formatted output. What you think you need is a method to pad spaces, but that's not necessary.~
Ok maybe I didnt read into it far enough, the format method will work for my purposes. What I was actually looking for was an exact replacement for the Space(#) method I used in Xbase, so the actual method I had been using was this:
public String space( int spaces )
{
int n;
String spaced = "\u0020";
for( n = 1; n < spaces; n++ )
{
spaced = spaced + "\u0020";
}
return spaced;
}
Thanks for pointing ( and re-pointing ) me in the right direction. Dukes have been awarded.
BYW, if your goal was to generate formatted strings rather than generate console output,
you can work directly with java.util.Formatter:
import java.util.*;
public class FormatExample {
public static void main(String[] args) {
String[][] data = {
{"1","999999999"},
{"22","88888888"},
{"333","7777777"},
{"4444","666666"},
{"55555","55555"},
{"666666","4444"},
{"7777777","333"},
{"88888888","22"},
{"999999999","1"},
};
StringBuilder b = new StringBuilder();
Formatter f = new Formatter(b);
for(String[] row : data) {
f.format("|%-10s|%10s|", row[0], row[1]);
String output = b.toString();
b.delete(0, b.length()); //clear
System.out.println(output);
//System.out.format("|%-10s|%10s|%n", row[0], row[1]);
}
}
}
> BYW, if your goal was to generate formatted strings
> rather than generate console output,
> you can work directly with java.util.Formatter:
Why do all that when this works just as well
import java.util.*;
public class FormatExample {
public static void main(String[] args) {
String[][] data = {
{"1","999999999"},
{"22","88888888"},
{"333","7777777"},
{"4444","666666"},
{"55555","55555"},
{"666666","4444"},
{"7777777","333"},
{"88888888","22"},
{"999999999","1"},
};
for(String[] row : data) {
String output = String.format("|%-10s|%10s|", row[0], row[1]);
System.out.println(output);
}
}
~Tim
Yes I'm actually formatting strings to build a list of items and I want the 2nd columns to all line up properly like:123Item4557 Item 212Item 3So, thanks again, I'll play around with this info and see what works the best.
String output = String.format("|%-10s|%10s|", row[0], row[1]);
Good point -- I forgot about that chappie. It's still good to know
about the actual Formatter class if you want to accumulate
several formatted strings in a buffer or if your output target is a file.
> Why do all that when this works just as well
Why do all that when System.out.printf() works just as well?
for(String[] row : data) {
System.out.printf("|%-10s|%10s|\n", row[0], row[1]);
}
;o)
~