Padding stl strings in C++

后端 未结 12 1810
一向
一向 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 22:52

    I was looking the topic because Im developing VCL; Anyway making a function wasn't not so hard.

    void addWhiteSpcs(string &str, int maxLength) {
        int i, length;
    
        length = str.length();
        for(i=length; i

    In both cases it will add to the right 10 blank spaces. I Recomend to use monospace fonts like courier or consolas for a correct format.

    This is what happens when you're not using monospace font
    johnny____
    cash______

    // using monospace font the output will be
    johnny____
    cash______
    

    Both cases have the same length.

提交回复
热议问题