A string composed of a character repeated x number of times

S

Sathyaish

In Java, what function/method do we have for constructing composed of
a single character repeated x number of times.

I am looking for the .NET equivalant of this String class constructor:

public String(char c, int numTimesToRepeat);

I looked at the java.lang.String class' constructors and couldn't find
one that matched my needs.
 
A

Andrew T.

Sathyaish said:
In Java, what function/method do we have for constructing composed of
a single character repeated x number of times. ...
I looked at the java.lang.String class' constructors and couldn't find
one that matched my needs.

Generally when building String's in Java,
we would use StringBuffer() in a loop.
You might use a String+="a" in a loop,
but doing so is bad for memory reasons.

I have never needed a long string of all one
character, what are they good for?
(The only thing I can think of is a pseudo
section-separator in a text document, but
I would generally write the text as HTML
and use an <HR> as separator)
 
M

Michael Rauscher

Sathyaish said:
In Java, what function/method do we have for constructing composed of
a single character repeated x number of times.

I am looking for the .NET equivalant of this String class constructor:

public String(char c, int numTimesToRepeat);

I looked at the java.lang.String class' constructors and couldn't find
one that matched my needs.

There was a discussion in de.comp.lang.java some time ago. The "nicest"
solution there was:

String s = new String(new char[numTimesToRepeat]).replace((char)0, c);

Bye
Michael
 
S

Sathyaish

Thanks, Andy.

I understand the need for StringBuffer and StringBuilder classes.

The function/method/constructor I am looking is helpful in situations
where you want to format output on the stdout. If you want to display
a variable number of control characters depending on the length of a
string which you do not know, say, something input by the user.
 
A

Andrew T.

Sathyaish said:
Thanks, Andy.

You can show your appreciation, by remembering
that my name is Andrew.
The function/method/constructor I am looking is helpful in situations
where you want to format output on the stdout. If you want to display
a variable number of control characters depending on the length of a
string which you do not know, say, something input by the user.

System.out.printf()

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
 
L

Lew

Sathyaish said:
There was a discussion in de.comp.lang.java some time ago. The "nicest"
solution there was:

Michael said:
String s = new String(new char[numTimesToRepeat]).replace((char)0, c);

The replace() has a test in it for (char) 0 at each array position.

Test-free:

char [] a = new char [numTimesToRepeat];
Arrays.fill( a, c );
String s = new String( a );

Less sexy but more efficient.
 
K

Knute Johnson

Lew said:
Sathyaish said:
There was a discussion in de.comp.lang.java some time ago. The
"nicest" solution there was:

Michael said:
String s = new String(new char[numTimesToRepeat]).replace((char)0, c);

The replace() has a test in it for (char) 0 at each array position.

Test-free:

char [] a = new char [numTimesToRepeat];
Arrays.fill( a, c );
String s = new String( a );

Less sexy but more efficient.

Sometimes you might want to start with a String rather than character.
I've had this in my tricks bag for a long time.

public class StringSet {
public static String set(String str, int n) {
StringBuilder sb = new StringBuilder(n);
for (int i=0; i<n; i++)
sb.append(str);
return sb.toString();
}
}
 
I

impaler

In Java, what function/method do we have for constructing composed of
a single character repeated x number of times.

I am looking for the .NET equivalant of this String class constructor:

public String(char c, int numTimesToRepeat);

I looked at the java.lang.String class' constructors and couldn't find
one that matched my needs.


Try StringUtils from the jakarta commons project.
http://jakarta.apache.org/commons/lang/

All you have to do is:
String myStr = StringUtils.leftPad("", 3, 'z'); //result myStr =
"zzz"

HTH
 
R

Roedy Green

In Java, what function/method do we have for constructing composed of
a single character repeated x number of times.

I am looking for the .NET equivalant of this String class constructor:

public String(char c, int numTimesToRepeat);

I looked at the java.lang.String class' constructors and couldn't find
one that matched my needs.

see StringTools.rep, part of the common11 package.

See http://mindprod.com/products1.html#COMMON11
 
D

Daniel Pitts

Michael said:
String s = new String(new char[numTimesToRepeat]).replace((char)0, c);
The replace() has a test in it for (char) 0 at each array position.
Test-free:

char [] a = new char [numTimesToRepeat];
Arrays.fill( a, c );
String s = new String( a );
Less sexy but more efficient.

Sometimes you might want to start with a String rather than character.
I've had this in my tricks bag for a long time.

public class StringSet {
public static String set(String str, int n) {
StringBuilder sb = new StringBuilder(n);
for (int i=0; i<n; i++)
sb.append(str);
return sb.toString();
}

}

Better yet
public static String repeatedToString(Object obj, int times) {
final String value = String.valueOf(Obj);
StringBuilder builder = new StringBuilder(times * value.size());
for (String v: Collections.nCopies(value, times) {
builder.append(v);
}
return builder.toString();
}
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top