A string composed of a character repeated x number of times

Discussion in 'Java' started by Sathyaish, Apr 4, 2007.

  1. Sathyaish

    Sathyaish Guest

    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.
    Sathyaish, Apr 4, 2007
    #1
    1. Advertising

  2. Sathyaish

    Andrew T. Guest

    Sathyaish wrote:
    >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)

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

    Message posted via http://www.javakb.com
    Andrew T., Apr 4, 2007
    #2
    1. Advertising

  3. Sathyaish wrote:
    > 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
    Michael Rauscher, Apr 4, 2007
    #3
  4. Sathyaish

    Sathyaish Guest

    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, Apr 4, 2007
    #4
  5. Sathyaish

    Sathyaish Guest

    Thanks, Mike. Guess I'll just put in a loop in a custom method.
    Sathyaish, Apr 4, 2007
    #5
  6. Sathyaish

    Andrew T. Guest

    Sathyaish wrote:
    >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
    Andrew T., Apr 4, 2007
    #6
  7. On 04.04.2007 12:57, Andrew T. wrote:
    > Sathyaish wrote:
    >> 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()


    + DecimalFormat and others.

    robert
    Robert Klemme, Apr 4, 2007
    #7
  8. Sathyaish

    Lew Guest

    Sathyaish wrote:
    >> 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:


    Michael Rauscher wrote:
    > 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.

    --
    Lew
    Lew, Apr 4, 2007
    #8
  9. Lew wrote:
    > Sathyaish wrote:
    >>> 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:

    >
    > Michael Rauscher wrote:
    >> 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();
    }
    }

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Apr 4, 2007
    #9
  10. Sathyaish

    impaler Guest

    On Apr 4, 10:42 am, "Sathyaish" <> wrote:
    > 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
    impaler, Apr 4, 2007
    #10
  11. Sathyaish

    Roedy Green Guest

    On 4 Apr 2007 02:42:41 -0700, "Sathyaish" <> wrote,
    quoted or indirectly quoted someone who 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.


    see StringTools.rep, part of the common11 package.

    See http://mindprod.com/products1.html#COMMON11
    --
    Canadian Mind Products, Roedy Green, http://mindprod.com
    Priorities: Prevent global climate destabilisation. End both wars. Prepare for oil shortages.
    Roedy Green, Apr 4, 2007
    #11
  12. Sathyaish

    Daniel Pitts Guest

    On Apr 4, 9:13 am, Knute Johnson <>
    wrote:
    > Lew wrote:
    > > Sathyaish wrote:
    > >>> 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:

    >
    > > Michael Rauscher wrote:
    > >> 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();
    > }
    >
    > }
    >
    > --
    >
    > 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();
    }
    Daniel Pitts, Apr 4, 2007
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. iksrazal
    Replies:
    2
    Views:
    302
    Marcin Grunwald
    Mar 8, 2005
  2. Francesc Guim Bernat

    Defining composed keys [XSD]

    Francesc Guim Bernat, Jul 29, 2003, in forum: XML
    Replies:
    0
    Views:
    460
    Francesc Guim Bernat
    Jul 29, 2003
  3. lvcargnini

    Matrix composed by two matrix

    lvcargnini, Jul 4, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,640
    Jonathan Bromley
    Jul 5, 2006
  4. candide
    Replies:
    6
    Views:
    218
    candide
    Apr 1, 2011
  5. libsfan01
    Replies:
    7
    Views:
    108
    Dr John Stockton
    Aug 13, 2006
Loading...

Share This Page