c++ - 是否有一个 std::string 函数来附加空格直到给定的缩进级别

标签 c++ string std

我正在尝试获得以下效果:

This is a message            []
This is a different message  []
Short                        []
LOOOOOOOOOOOOOOOOOOOOOOOOOONG[]

有没有办法创建一个字符串,以便给定的参数始终以给定的缩进级别开始(如果可能)?

例如字符串(缩进级别,消息1,消息2)

最佳答案

标准库中不存在类似的东西(据我所知),但您应该能够轻松编写一些实现此目的的东西。

std::string IndentedString(int n, std::string message) {

    // Construct string of 'n - message.length' spaces
    std::string indent(n - message.length, ' ');

    return message + indent;
}

关于c++ - 是否有一个 std::string 函数来附加空格直到给定的缩进级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49850104/

相关文章:

c++ - 将 std::__cxx11::string 转换为 std::string

Python-在字符串中插入一个字符

c++ - 无法使用 std::move move 内容

c++ - 控制台崩溃,无法识别错误?

c++ - Apache 模块命令解析器原型(prototype)

c++ - 混合不会消除 OpenCV 中的接缝

c++ - 使用 IImageEncoder 将图像保存到文件

c# - 如何消除字符串中的所有换行符?

c# - 解析大字符串的最快方法(多线程)

c++ - std::cout 不适用于结构的重载 '<<' 运算符