How to repeat a string a variable number of times in C++?

c++

Question

I want to insert 'n' spaces (or any string) at the beginning of a string in C++. Is there any direct way to do this using either std::strings or char* strings?

E.g. in Python you could simply do

>>> "." * 5 + "lolcat"
'.....lolcat'
1
100
10/3/2008 12:43:04 PM

In the particular case of repeating a single character, you can use std::string(size_type count, CharT ch):

std::string(5, '.') + "lolcat"

NB. This can't be used to repeat multi-character strings.

142
3/25/2019 5:06:08 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon