Prev: Korean language broken
Next: Possible easy diagnostic outputting from multiple threads to the one text frame
From: Lew on 13 Jul 2010 17:28 Eric Sosman wrote: > -- but there's a saying "Don't use a cannon to kill a canary," ... > Unless it's a REALLY big canary. Q: What does a 500-pound (227-kg) canary say? A: *CHIRP*!! -- Lew
From: Andreas Leitgeb on 13 Jul 2010 18:31 Eric Sosman <esosman(a)ieee-dot-org.invalid> wrote: > 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: >>>> - 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); Yep, of course. I'd choose the latter one, if I got round to create a helper method for that task, but use the former if I just inline it somewhere. But one would still need to decide on the maximum length beforehand. > 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 ... Indeed :-) So would new String(new char[n]).replace('\0',ch), creating two extra objects. And char[] tmp=new char[n]; java.util.Arrays.fill(tmp,ch); ...= new String(tmp); (why does Arrays.fill not return a ref to it's argument?) A more elaborate, but likely not really paying approach: :-) public class BoringString implements CharSequence { private char m_ch; private int m_len; public BoringString(int len, char ch) { m_ch=ch; m_len=len; } public char charAt(int index) { return m_ch; } public int length() { return m_len; } public BoringString subSequence(int start, int end) { return new BoringString(end-start,m_ch); } public String toString() { return new StringBuilder(this).toString(); } // that seems to work: SB doesn't use toString() on its arg. } PS: At least the apache-people thought of rightPad(String,int,char)
From: Andreas Leitgeb on 13 Jul 2010 18:36 Jim Janney <jjanney(a)shell.xmission.com> wrote: > 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 > http://commons.apache.org/lang/api-release/org/apache/commons/lang/StringUtils.html#leftPad(java.lang.String,%20int,%20char) My rant was about the Java Standard Library, but still: thanks for the link! (it's good to know that those useful helpers at least exist somewhere else).
From: Arne Vajhøj on 13 Jul 2010 19:23 On 13-07-2010 11:01, Andreas Leitgeb 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. I don't think so. Apparently they forgot about this useful feature. Arne
From: Roedy Green on 14 Jul 2010 11:56 On 13 Jul 2010 15:01:35 GMT, Andreas Leitgeb <avl(a)gamma.logic.tuwien.ac.at> wrote, quoted or indirectly quoted someone who said : >It seems so basic that I can't believe such a feature wasn't in >the standard library: it is part of the common11 tools for JDK 1.1+ http://mindprod.com/products1.html#COMMON11 The method is called StringTools.rep /** * Produce a String of a given repeating character. * * @param c the character to repeat * @param count the number of times to repeat * * @return String, e.g. rep('*',4) returns "****" * @noinspection WeakerAccess,SameParameterValue */ public static String rep( char c, int count ) { if ( c == ' ' && count <= SOMESPACES.length() ) { return SOMESPACES.substring( 0, count ); } char[] s = new char[count]; for ( int i = 0; i < count; i++ ) { s[ i ] = c; } return new String( s ).intern(); } /** * used to efficiently generate Strings of spaces of varying length */ private static final String SOMESPACES = " "; -- Roedy Green Canadian Mind Products http://mindprod.com You encapsulate not just to save typing, but more importantly, to make it easy and safe to change the code later, since you then need change the logic in only one place. Without it, you might fail to change the logic in all the places it occurs.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: Korean language broken Next: Possible easy diagnostic outputting from multiple threads to the one text frame |