Java equivalent to VB 'string' function (create string with repeatingcharacters)?

O

ohaya

Hi,

Is there an equivalent in Java to the VB 'string' function, which
creates a string of 'x' repeating characters?

For instance, something like:

String s = vbEquivString("x", 6); // yields s = "xxxxxx"

Thanks,
Jim
 
V

Vova Reznik

ohaya said:
Hi,

Is there an equivalent in Java to the VB 'string' function, which
creates a string of 'x' repeating characters?

For instance, something like:

String s = vbEquivString("x", 6); // yields s = "xxxxxx"

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();
 
R

Roedy Green

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
 
O

ohaya

Roedy said:
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


Vova and Roedy,

Thanks again!!!

Jim
 
T

Thomas Hawtin

Roedy said:
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
 
R

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top