3

Possible Duplicate:
Can I multiply strings in java to repeat sequences?

In Python, we can easily multiply the stings.

count = 10
print '*' * count

Is there any similar option available in Java?

Community
  • 1
  • 1
Siva Arunachalam
  • 7,582
  • 15
  • 79
  • 132

4 Answers4

6

You can use Dollar for your purposes(Java API that unifies collections, arrays, iterators/iterable, and char sequences.)

String str = $("*").repeat(count); 

In this way you will get result "**********" as you want.

Java doesn't have that feature for now.

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
6
char[10] c = new char[10];
Arrays.fill(c, '*');
String str = new String(c);

To avoid creating a new String everytime.

LazyCubicleMonkey
  • 1,213
  • 10
  • 17
4

How about this??

System.out.println(String.format("%10s", "").replace(' ', '*'));

This gives me output as **********.

I believe this is what you want...

Update 1

int yournumber = 10;
System.out.println(String.format("%" + yournumber + "s","*").replace(' ', '*'));

Good Luck!!!

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
0

The simplest way to do that in Java is to use a for() loop:

String s = "";
for (int i = 0; i < 10; i++) {
    s += "*";
}
System.out.println(s);
seeming.amusing
  • 1,179
  • 1
  • 8
  • 18
  • 1
    I would prefer mesiesta answer over you... – Fahim Parkar Feb 13 '12 at 06:49
  • 1
    It makes everytime new string, because strings are immutable in Java. I will suggest to use Dollar or use StringBuilder class instead of String. – Chuck Norris Feb 13 '12 at 06:50
  • @mesiesta you're right that a new `String` is created every time but not because the immutability of `String`. Rather because each iteration creates a _different_ `String` than the previous ones. E.g. one-star, two-stars, three-stars, etc. – yair Feb 13 '12 at 07:13
  • This is an abominable solution. Seriously. @yair you're definitely wrong here. It creates a new string because they're immutable and s cannot be appended to it must get recreated. In fact all string modifications require recreating the string because they are immutable. – fIwJlxSzApHEZIl Mar 27 '19 at 18:01
  • @anon58192932 reading this more than 7 years later, this is a somewhat philosophical argument, what is the exact cause for the creation of new strings every iteration. `s += "*"` is syntactic sugar of `s = s + "*"`. AFAIR, the runtime first executes `s + "*"`, which is done regardless of the assignment. The result is first assigned to a new address, not `s`'s. Then `s` is assigned that new address. So, in this regard, the immutability of strings is not the cause for creating new strings. The cause is just the order of instructions execution. – yair Mar 27 '19 at 19:24
  • @anon58192932 it's agreed that the solution is not ideal, but please avoid using unpleasant words like "abominable". – yair Mar 27 '19 at 19:25
  • it's the truth. never shy away from the truth. abominable: very bad or unpleasant. This solution is both of those and should be deleted. – fIwJlxSzApHEZIl Mar 29 '19 at 23:28