From: Andreas Leitgeb on
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.

From: Kevin McMurtrie on
In article <slrni3ovuf.sno.avl(a)gamma.logic.tuwien.ac.at>,
Andreas Leitgeb <avl(a)gamma.logic.tuwien.ac.at> wrote:

> 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.

Homework?

It's easy when the pad character is a constant:

static String padToDigits (final String s, final int digits)
{
final int len= s.length();
if (len > digits)
throw new IllegalArgumentException ("Input too long:" + s);
if (digits > 40)
throw new IllegalArgumentException ("Too many digits:" + digits);
if (len == digits)
return s;
return "0000000000000000000000000000000000000000"
.substring(0, digits - len) + s;
}

When it's not, a for loop to pad a StringBuidler works fine.
--
I won't see Google Groups replies because I must filter them as spam
From: Jim Janney on
Andreas Leitgeb <avl(a)gamma.logic.tuwien.ac.at> writes:

> 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.

http://commons.apache.org/lang/api-release/org/apache/commons/lang/StringUtils.html#leftPad(java.lang.String,%20int,%20char)

--
Jim Janney
From: Andreas Leitgeb on
Kevin McMurtrie <mcmurtrie(a)pixelmemory.us> wrote:
> Andreas Leitgeb <avl(a)gamma.logic.tuwien.ac.at> wrote:
>> It seems so basic that I can't believe such a feature wasn't in
>> the standard library:
>> 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.

> It's easy when the pad character is a constant:

My request was for a *simple* idiom. I explicitly wrote that I did
know about using .substring() on a constant string and called it a crude
hack (for its imposed limit on n and c).

> When it's not, a for loop to pad a StringBuidler works fine.

That's one of the other strategies I mentioned as not much better.

Thanks for trying to help, but that didn't yet.
From: Eric Sosman on
On 7/13/2010 3:54 PM, Andreas Leitgeb wrote:
> Kevin McMurtrie<mcmurtrie(a)pixelmemory.us> wrote:
>> Andreas Leitgeb<avl(a)gamma.logic.tuwien.ac.at> wrote:
>>> It seems so basic that I can't believe such a feature wasn't in
>>> the standard library:
>>> 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.

One easy way to write "the appropriate substring" is to
concatenate and *then* cut:

String padded = (original + padChars).substring(0, n);

This costs two String creations instead of (at most) one, but is
certainly less verbose than

String padded = (original.length() < n)
? original + padChars.substring(0, n-original.length())
: original.substring(0, n);

As for finding anything simpler that's built-in ... Well, there
*might* be some way to use String.format() to do it -- but there's
a saying "Don't use a cannon to kill a canary," and .format() looks
quite a lot like a cannon here ...

--
Eric Sosman
esosman(a)ieee-dot-org.invalid