Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

A string composed of a character repeated x number of times

Thread view: 
Sathyaish - 04 Apr 2007 10:42 GMT
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.
Andrew T. - 04 Apr 2007 10:55 GMT
>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)

Signature

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

Sathyaish - 04 Apr 2007 11:01 GMT
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.
Sathyaish - 04 Apr 2007 11:17 GMT
Thanks, Mike. Guess I'll just put in a loop in a custom method.
Andrew T. - 04 Apr 2007 11:57 GMT
>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()

Signature

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

Robert Klemme - 04 Apr 2007 12:16 GMT
>> Thanks, Andy.
>
[quoted text clipped - 7 lines]
>
> System.out.printf()

+ DecimalFormat and others.

    robert
Michael Rauscher - 04 Apr 2007 10:55 GMT
> In Java, what function/method do we have for constructing composed of
> a single character repeated x number of times.
[quoted text clipped - 5 lines]
> 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
Lew - 04 Apr 2007 13:16 GMT
Sathyaish wrote:
>> In Java, what function/method do we have for constructing composed of
>> a single character repeated x number of times.
[quoted text clipped - 8 lines]
> 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);

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.

Signature

Lew

Knute Johnson - 04 Apr 2007 17:13 GMT
> Sathyaish wrote:
>>> In Java, what function/method do we have for constructing composed of
[quoted text clipped - 21 lines]
>
> 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();
    }
}

Signature

Knute Johnson
email s/nospam/knute/

Daniel Pitts - 04 Apr 2007 20:03 GMT
On Apr 4, 9:13 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> > Sathyaish wrote:
> >>> In Java, what function/method do we have for constructing composed of
[quoted text clipped - 39 lines]
> Knute Johnson
> email s/nospam/knute/

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();
}
impaler - 04 Apr 2007 17:28 GMT
> In Java, what function/method do we have for constructing composed of
> a single character repeated x number of times.
[quoted text clipped - 5 lines]
> 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
Roedy Green - 04 Apr 2007 19:31 GMT
>In Java, what function/method do we have for constructing composed of
>a single character repeated x number of times.
[quoted text clipped - 5 lines]
>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
Signature

Canadian Mind Products, Roedy Green, http://mindprod.com
Priorities: Prevent global climate destabilisation. End both wars. Prepare for oil shortages.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.