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?
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?
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.
char[10] c = new char[10];
Arrays.fill(c, '*');
String str = new String(c);
To avoid creating a new String everytime.
How about this??
System.out.println(String.format("%10s", "").replace(' ', '*'));
This gives me output as **********
.
I believe this is what you want...
int yournumber = 10;
System.out.println(String.format("%" + yournumber + "s","*").replace(' ', '*'));
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);