> Hi,
>
[quoted text clipped - 7 lines]
> Thanks,
> Jim
char[]arr = nea char[size];
Arrays.fill(arr, 'x');
String s = new vbEquivString(arr);
or
StringBuffer sb = new StringBuffer(size);
for(int i=0; i<sb.capacity(); i++){
sb.append('x');
}
String s = sb.toString();
>String s = vbEquivString("x", 6); // yields s = "xxxxxx"
The .intern is in case you are producing the same strings over and
over.
/**
* 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 "****"
*/
public final static String rep ( char c, int count )
{
char[] s = new char[ count ];
for ( int i = 0; i < count; i++ )
{
s[ i ] = c;
}
return new String( s ).intern();
} // end rep

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
ohaya - 17 Oct 2005 22:40 GMT
> >String s = vbEquivString("x", 6); // yields s = "xxxxxx"
>
[quoted text clipped - 18 lines]
> return new String( s ).intern();
> } // end rep
Vova and Roedy,
Thanks again!!!
Jim
Thomas Hawtin - 19 Oct 2005 02:25 GMT
> The .intern is in case you are producing the same strings over and
> over.
> return new String( s ).intern();
To me, interning doesn't seem like the safe thing to do.
If the method is called often for the same values, then the string
itself will almost certainly be short lived. Interning just adds an
expensive operation. The additional GC is also particularly expensive,
but will be difficult to spot with a profiler.
If the method is called once and the result kept, then it doesn't really
matter where the string is.
There is a quite fun way to avoid excessive overheads when creating
varying length string of the same character repeated. Recall that
String.substring shares the underlying char array.
private String zeros = "0000";
public static String zeros(final int len) {
if (len < 0) {
throw new IllegalArgumentException()
}
for (;;) {
final String zeros = ThisClass.zeros;
if (zeros.length >= len) {
return zeros.substring(0, len);
}
ThisClass.zeros = zeros+zeros;
}
}
Technically this requires Java 5.0 in order to be thread-safe.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Roedy Green - 19 Oct 2005 03:41 GMT
>There is a quite fun way to avoid excessive overheads when creating
>varying length string of the same character repeated. Recall that
>String.substring shares the underlying char array.
I used that technique in the left zero padding method. The catch is it
does to work for a general repeat char.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
ohaya - 20 Oct 2005 02:12 GMT
> >There is a quite fun way to avoid excessive overheads when creating
> >varying length string of the same character repeated. Recall that
> >String.substring shares the underlying char array.
>
> I used that technique in the left zero padding method. The catch is it
> does to work for a general repeat char.
Hi all,
I'm the original poster. FYI, what I was really looking to find out
originally was if there was a 'built-in' function to do this in Java.
From the responses, I gather that there isn't, so I've tried a couple of
the suggested methods, which all seemed to have worked, so again,
thanks!
Jim