Prev: Korean language broken
Next: Possible easy diagnostic outputting from multiple threads to the one text frame
From: voorth on 14 Jul 2010 12:20 How about using Arrays.fill() ? public static String rep(char c, int count) { char[] chars = new char[count] ; Arrays.fill(chars, c); return new String(chars); } On Jul 14, 5:56 pm, Roedy Green <see_webs...(a)mindprod.com.invalid> wrote: > On 13 Jul 2010 15:01:35 GMT, Andreas Leitgeb > <a...(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 Productshttp://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.
From: Kevin McMurtrie on 14 Jul 2010 12:42 In article <sbnr36ta3l2bqkf0mt23pgg6g3k8nf2ud2(a)4ax.com>, Roedy Green <see_website(a)mindprod.com.invalid> wrote: > 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 = " "; Why use intern() on the second case? It's has always been undocumented where the pool storage is and what the cost of using it is. The only time I use that method is when generating keys for a Properties class. -- I won't see Google Groups replies because I must filter them as spam
From: Tom Anderson on 14 Jul 2010 15:43 On Tue, 13 Jul 2010, Eric Sosman wrote: > 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 ... I thought the traditional way to kill canaries was the throw them down coalmines. tom -- blackguard, coward, git, guttersnipe, hooligan, ignoramus, liar, rat, swine, stoolpigeon, and traitor
From: Tom Anderson on 14 Jul 2010 18:02 On Tue, 13 Jul 2010, Andreas Leitgeb wrote: > 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 The standard library is already freak huge. If it had every item which anyone couldn't believe wasn't in the standard library, it could be a very leviathan, a behemoth, a colossus of codes. What i think we could do with is a sort of Greater Standard Library. Some way of giving certain packages official blessing, so that if you built code on top of them, you'd have a reasonable expectation that someone out there who wanted to run your code would already have them. This already exists de facto for certain things - log4j, quite a bit of Apache Commons, JUnit, and so on, but it could be useful to put it on a more formal, although not completely formal, basis. We could tie this up with a process for merging different libraries approaching the same problem, reviewing and improving and integrating things, etc. We would end up with something like a more comprehensive standard library, but without the mandarins at Sunacle (or JCP Towers - shudder) having to decree it. Not that i'm volunteering to organise this, of course. tom -- blackguard, coward, git, guttersnipe, hooligan, ignoramus, liar, rat, swine, stoolpigeon, and traitor
From: Peter Duniho on 14 Jul 2010 19:20 Tom Anderson wrote: > On Tue, 13 Jul 2010, Andreas Leitgeb wrote: > >> 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 > > The standard library is already freak huge. If it had every item which > anyone couldn't believe wasn't in the standard library, it could be a > very leviathan, a behemoth, a colossus of codes. [...] That's true. But it doesn't explain why there's not a constructor for the String class that takes a character and/or string and a count, to initialize the String instance with a number of repetitions of said character and/or string equal to the count. Such a constructor would hardly be outside the existing scope of the type, nor would it in any significant way contribute to the problem of the size of the standard library. There are lots of things that are esoteric enough and complicated enough that they certainly should not be a part of the standard library, for a variety of reasons including the basic reason that you don't want a standard library to have so much stuff it's not manageable. But initializing strings with specific counts of repeated text isn't in that group of things. Pete
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 |