views:

350

answers:

8

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".

+3  A: 

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, 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);
LES2
how come you assumed he is using maven? :)
Bozho
Cuz everyone is, and you're not cool if you aren't!
GreenieMeanie
You have to make some assumptions in order to solve real world problems. In this case, I'm using maven on my current project. I have a pom.xml set up with the commons-lang dependency. It made it easier for me to answer his question, in short. And I'm not exactly sure what I'd do if I wasn't using maven - I guess I'd have to create a custom location for my jar files ... similar to a maven repository! And then include those jar files on my classpath and do all kinds of work I like to avoid. If the poster really wants to hack java, then she or he needs to use maven or its equivalent. :)
LES2
+4  A: 

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

Geo
String multiplyString(String str, int count){ StringBuilder sb = new StringBuilder(); for( int i = 0; i < count; ++i ){ sb.append(str); } return sb.toString(); }
Vincent Robert
StringUtils has been performance tuned and tested. The time to produce a production quality String utility library is time you don't have unless your current project is to write a production quality String utility library. Don't write code you don't have to. Plus, there are many other useful methods in StringUtils and commons-lang you can use. It's worth the dependency.
LES2
Chill. He was asking how it could be done. He wasn't asking for a jar.
Geo
A: 

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.

Vivin Paliath
A: 

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";
}
royalsampler
You might want to use StringBuffer when concatenating string several times.
tou
+2  A: 

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.

BalusC
Way 1 only works with single characters, Way 2 only works with a single '0'. Not exactly what I'd call a generic solution to the problem...
hjhill
Not sure what you're talking about, but it's at least a suitable solution for the OP's own problem :) If you have a problem, ask a question.
BalusC
A: 

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 < times;i++){
        b.append(c);
    }

    return b.toString();
}

Disclaimer: I typed it here. Might have mistakes

tou
The version of repeat in StringUtils has been performance tested on different JVMS. E.g., inspection of it's source code reveals the following comment: "// Performance tuned for 2.0 (JDK1.4)". Now if you look at the implementation, it's pretty hairy - it has all sorts of checks for null and empty String and other optimizations. And it's presumably covered by comprehensive unit tests. The first rule of programming should be Don't Write Code.
LES2
A: 

With Guava:

Joiner.on("").join(Collections.nCopies(i, someNum));
finnw
A: 

with Dollar:

String s = "123" + $("0").repeat(3); // 123000
dfa