I need my string to repeat n amount of times, something like this:
QString s("Dog");
qDebug() << s * 3;
"DogDogDog"
I know you can do it with single char
's, but I can't figure out how to do it with strings, without resorting to creating a for loop like this:
https://paste.fedoraproject.org/300131/94336814/
Any shortcuts?
QString
simply has not such an operator (see the documentation), so you can't use operator*
to do that.
Anyway, QString
has an interesting method called repeated.
I cite the documentation, that is quite exhaustive:
Returns a copy of this string repeated the specified number of times.
If times is less than 1, an empty string is returned.
It follows an example, once more from the official documentation:
QString str("ab");
str.repeated(4); // returns "abababab"
I guess this solves your problem and it seems to be the more concise solution available.