Can I multiply strings in Java to repeat sequences?

0 votes
asked Feb 12, 2010 by mithrax

I have something like the following:

int i = 3;
String someNum = "123";

I'd like to append i "0"s to the someNum string. Does it have some way I can multiply a string to repeat it like Python does?

So I could just go:

someNum = sumNum + ("0" * 3);

or something similar?

Where, in this case, my final result would be:

"123000".

18 Answers

0 votes
answered by andi

The easiest way in plain Java with no dependencies is the following one-liner:

new String(new char[generation]).replace("\0", "-")

Replace generation with number of repetitions, and the "-" with the string (or char) you want repeated.

All this does is create an empty string containing n number of 0x00 characters, and the built-in String#replace method does the rest.

Here's a sample to copy and paste:

public static String repeat(int count, String with) {
    return new String(new char[count]).replace("\0", with);
}

public static String repeat(int count) {
    return repeat(count, " ");
}

public static void main(String[] args) {
    for (int n = 0; n < 10; n++) {
        System.out.println(repeat(n) + " Hello");
    }

    for (int n = 0; n < 10; n++) {
        System.out.println(repeat(n, ":-) ") + " Hello");
    }
}
0 votes
answered Jan 10, 2010 by arbaaz

The simplest way is:

String someNum = "123000";
System.out.println(someNum);
0 votes
answered Jan 12, 2010 by vivin-paliath

I don't believe Java natively provides this feature, although it would be nice. I write Perl code occasionally and the x operator in Perl comes in really handy for repeating strings!

However StringUtils in commons-lang provides this feature. The method is called repeat(). Your only other option is to build it manually using a loop.

0 votes
answered Jan 12, 2010 by geo

No. Java does not have this feature. You'd have to create your String using a StringBuilder, and a loop of some sort.

0 votes
answered Jan 12, 2010 by les2

No, but you can in Scala! (And then compile that and run it using any Java implementation!!!!)

Now, if you want to do it the easy way in java, use the Apache commons-lang package. Assuming you're using maven, add this dependency to your pom.xml:

    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.4</version>
    </dependency>

And then use StringUtils.repeat as follows:

import org.apache.commons.lang.StringUtils
...
someNum = sumNum + StringUtils.repeat("0", 3);
0 votes
answered Jan 12, 2010 by royalsampler

There's no shortcut for doing this in Java like the example you gave in Python.

You'd have to do this:

for (;i > 0; i--) {
    somenum = somenum + "0";
}
0 votes
answered Jan 12, 2010 by balusc

Two ways comes to mind:

int i = 3;
String someNum = "123";

// Way 1:
char[] zeroes1 = new char[i];
Arrays.fill(zeroes1, '0');
String newNum1 = someNum + new String(zeroes1);
System.out.println(newNum1); // 123000

// Way 2:
String zeroes2 = String.format("%0" + i + "d", 0);
String newNum2 = someNum + zeroes2;
System.out.println(newNum2); // 123000

Way 2 can be shortened to:

someNum += String.format("%0" + i + "d", 0);
System.out.println(someNum); // 123000

More about String#format() is available in its API doc and the one of java.util.Formatter.

0 votes
answered Jan 12, 2010 by omar

No, you can't. However you can use this function to repeat a character.

public String repeat(char c, int times){
    StringBuffer b = new StringBuffer();

    for(int i=0;i &lt; times;i++){
        b.append(c);
    }

    return b.toString();
}

Disclaimer: I typed it here. Might have mistakes.

0 votes
answered Jan 13, 2010 by finnw

With Guava:

Joiner.on("").join(Collections.nCopies(i, someNum));
0 votes
answered Jan 13, 2010 by dfa

with Dollar:

String s = "123" + $("0").repeat(3); // 123000
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...