Padding stl strings in C++

后端 未结 12 1870
一向
一向 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:14

    You can use it like this:

    std::string s = "123";
    s.insert(s.begin(), paddedLength - s.size(), ' ');
    

提交回复
热议问题