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

2
  • 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. Commented Mar 5, 2013 at 14:52
  • If you feel that your question was answered by an answer, please accept that answer. :) Commented Mar 6, 2013 at 12:06

4 Answers 4

8

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

str2 = Strings.repeat("hello", 10);
Sign up to request clarification or add additional context in comments.

Comments

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)

2 Comments

Beat me to the punch. However depending on the OP's needs he may need to weigh the pros/cons between StringBuilder and StringBuffer: javahowto.blogspot.ca/2006/08/…
Nice link you added there :)
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.

1 Comment

This is okay for 10 repetitions, but AFAIK, appending in String is a costly operation and StringBuffer is recommended instead.
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";
}

2 Comments

This is okay for 10 repetitions, but AFAIK, appending in String is a costly operation and StringBuffer is recommended instead.
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. ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.