A
Andreas Leitgeb
It seems so basic that I can't believe such a feature wasn't in
the standard library:
- given two parameters (int n, char c), return a String that
consists of <n> copies of <c>
(would be imho best suited as a constructor of String itself)
or (what I'd actually need it for
- given a String, an int n and a char c, cut or pad(with c) the String
to length n. (the cut/padded one would be a new String, of course)
(I'd need it right-padded, but the problem is the same for left-
padding)
As I know that the strings I need it for are of limited length, and
only a few pad-chars are ever needed, I can help myself, by using
a String constant, like "............................" for each pad-
char, and use an appropriate .substring on it. This sure looks like
a crude hack, but the alternatives involving StringBuffer(*) and
char[n] don't seem much better.
*: StringBuffer.setLength only pads with \u0000, and there doesn't
seem to be an append(numCopies,padChar), either, so one seems
to be bound to adding the same char repeatedly in a loop.
Did I miss some simple idiom? I really hope so.
the standard library:
- given two parameters (int n, char c), return a String that
consists of <n> copies of <c>
(would be imho best suited as a constructor of String itself)
or (what I'd actually need it for
- given a String, an int n and a char c, cut or pad(with c) the String
to length n. (the cut/padded one would be a new String, of course)
(I'd need it right-padded, but the problem is the same for left-
padding)
As I know that the strings I need it for are of limited length, and
only a few pad-chars are ever needed, I can help myself, by using
a String constant, like "............................" for each pad-
char, and use an appropriate .substring on it. This sure looks like
a crude hack, but the alternatives involving StringBuffer(*) and
char[n] don't seem much better.
*: StringBuffer.setLength only pads with \u0000, and there doesn't
seem to be an append(numCopies,padChar), either, so one seems
to be bound to adding the same char repeatedly in a loop.
Did I miss some simple idiom? I really hope so.