In Python, there is a *
operator for strings, I'm not sure what it's called but it does this:
>>> "h" * 9
"hhhhhhhhh"
Is there an operator in Java like Python's *
?
In Python, there is a *
operator for strings, I'm not sure what it's called but it does this:
>>> "h" * 9
"hhhhhhhhh"
Is there an operator in Java like Python's *
?
Many libraries have such utility methods.
E.g. Guava:
String s = Strings.repeat("*",9);
String s = StringUtils.repeat("*", 9);
Both of these classes also have methods to pad a String's beginning or end to a certain length with a specified character.
I think the easiest way to do this in java is with a loop:
String string = "";
for(int i=0; i<9; i++)
{
string+="h";
}
you can use something like this :
String str = "abc";
String repeated = StringUtils.repeat(str, 3);
repeated.equals("abcabcabc");
There is no such operator in Java, but you can use Arrays.fill() or Apache Commons StringUtils.repeat() to achieve that result:
Assuming
char src = 'x';
String out;
with Arrays.fill()
char[] arr = new char[10] ;
Arrays.fill(arr,src);
out = new String(arr);
with StringUtils.repeat()
out = StringUtils.repeat(src, 10);
Use apache libraries (common-lang) : Stringutils.repeat(str, nb)
There is no operator like that, but you could assign the sting "h" to a variable and use a for loop to print the variable a desired amount of times.