Align text to center in string C++

前端 未结 3 1426
既然无缘
既然无缘 2021-01-17 07:45

I have been trying to make a function in C++ for a gameserver DLL which aligns given text to center before returning the new string to Lua for processing. I have spent quite

3条回答
  •  不知归路
    2021-01-17 08:30

    std::string center (const std::string& s, unsigned width)
    {
        assert (width > 0);
        if (int padding = width - s.size (), pad = padding >> 1; pad > 0)
            return std::string (padding, ' ').insert (pad, s);
        return s;
    }
    

提交回复
热议问题