Padding stl strings in C++

后端 未结 12 1861
一向
一向 2020-12-05 22:15

I\'m using std::string and need to left pad them to a given width. What is the recommended way to do this in C++?

Sample input:

123
         


        
12条回答
  •  难免孤独
    2020-12-05 23:06

    string t = string(10, ' ') + to_string(123);
    cout << t.substr(t.size() - 10) << endl;
    

    Try it online!

提交回复
热议问题