9

I am a new to java coming from python. I am wondering how I can multiply a string in java. In python I would do this:

str1 = "hello"
str2 = str1 * 10

string 2 now has the value:

#str2 == 'hellohellohellohellohellohellohellohellohellohello'

I was wondering what the simplest way is to achieve this in java. Do I have to use a for loop or is there a built in method?

EDIT 1

Thanks for your responses I have since found an elegant solution to my problem:

str2 = new String(new char[10]).replace("\0", "hello");

note: this answer was originally posted by user102008 here: https://stackoverflow.com/a/4903603

Community
  • 1
  • 1
Alec Hewitt
  • 805
  • 1
  • 12
  • 21
  • 1
    No, there isn't, tough some frameworks/libraries offer that function. In your current context (and to **avoid adding libraries for a single small task** you can do yourself and learn in the process), a `StringBuffer` or `StringBuilder` and a `for` loop should suffice. – Fritz Mar 05 '13 at 14:52
  • If you feel that your question was answered by an answer, please accept that answer. :) – wei2912 Mar 06 '13 at 12:06

4 Answers4

8

Although not built-in, Guava has a short way of doing this using Strings:

str2 = Strings.repeat("hello", 10);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
3

You can use a StringBuffer.

String str1 = "hello";
StringBuffer buffer = new StringBuffer(str1);
for (int i = 0; i < 10; i++) {
    buffer.append(str1);
}
str2 = buffer.toString();

Refer to http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/StringBuffer.html for documentation.

If you're not going to use any threads, you can use StringBuilder which sort of works the same way as StringBuffer but is not thread-safe. Refer to http://javahowto.blogspot.ca/2006/08/stringbuilder-vs-stringbuffer.html for more details (thanks TheCapn)

wei2912
  • 6,141
  • 2
  • 19
  • 20
  • 2
    Beat me to the punch. However depending on the OP's needs he may need to weigh the pros/cons between `StringBuilder` and `StringBuffer`: http://javahowto.blogspot.ca/2006/08/stringbuilder-vs-stringbuffer.html – Grambot Mar 05 '13 at 14:53
  • Nice link you added there :) – wei2912 Mar 05 '13 at 15:00
2

The for loop is probably your best bet here:

for (int i = 0; i < 10; i++)
  str2 += "hello";

If you are doing a lot of iterations (in the 100+ range) consider the use of a StringBuilder object as each time you modify the string you are allocating new memory and releasing the old string for garbage collection. In cases where you are doing this a considerable amount of times it will be a performance issue.

Grambot
  • 4,370
  • 5
  • 28
  • 43
  • This is okay for 10 repetitions, but AFAIK, appending in `String` is a costly operation and `StringBuffer` is recommended instead. – aa8y Mar 05 '13 at 15:00
0

I don't think there is a way to do that without some sort of loop.

For example:

String result = "";
for (int i=0; i<10; i++){
    result += "hello";
}
Fritz
  • 9,987
  • 4
  • 30
  • 49
Peter Jaloveczki
  • 2,039
  • 1
  • 19
  • 35
  • This is okay for 10 repetitions, but AFAIK, appending in `String` is a costly operation and `StringBuffer` is recommended instead. – aa8y Mar 05 '13 at 15:01
  • Exactly so. I have previously tested the performance of each method, and unless you want the iteration to run like 10000 times, the performance you gain is minimal so the code might be more readable this way. That being said it is totally up the the developer to decide which one to use based on the statement above. I will still say though that EASIEST way is definatelly not to use some kind of 3rd party lib, because in the end it will at least do the same iteration and then probably more stuff you don't necessarily want. ;) – Peter Jaloveczki Mar 05 '13 at 15:10