在 std::string 的末尾插入换行符
Inserting a newline near the end of a std::string
我正在努力尝试实现一个C++代码解决方案,该解决方案将允许我在std::string
末尾插入换行符(即字符串文字'n'
(,而不是像大多数实现显示的那样在最后
。例如,我想在结尾本身之前插入一个'n'
,只-1
个字符。因此,如果字符串的长度为 100 个字符(我知道很糟糕的类比(,那么我想以干净、易于阅读的方式在第 99 个字符处插入字符串文字。
谢谢!
这是一种方法:
std::string test{"abcdef"};
if (!test.empty())
test.insert(test.length() - 1, "n");
这是一个基于迭代器的:
if (!test.empty())
test.insert(std::prev(test.end()), 'n');
相关文章:
- 如何防止clang格式在流运算符调用之间添加换行符<<
- C/C++ - 查询平台相关的换行符(用于内存映射文件)
- 在 Stream C++ 文本之前有一个额外的换行符
- 流:CSV 文件中的换行符
- 如何在三元条件运算符中添加换行符和连接? :在 C++ 中
- Clang-格式:在多行语句之后的换行符上打开大括号
- 在输出流中插入换行符
- 防止控制台在通道字符(在 c++ 中)中输入空格(即空格、制表符和换行符)
- QProcess::readAllStandardOutput() 和换行符
- 如果有换行符,clang 格式不会附加大括号
- 提取后返回换行符的C++istream.get()
- 如何在出现换行符之前将多个整数作为输入?
- 正则表达式 获取两个换行符之间的文本
- 包含换行符分隔的单词的文件和C++中这些单词的字符串向量的大小是否相同?
- 在 std::string 的末尾插入换行符
- QListWidget,插入换行符以获得较长的文本
- 编写一个C++ pgm 以从文件"input.txt"读取,每当遇到句点时插入换行符并将修改的内容写入"output.txt"
- 在字符数组中插入新的换行符
- QPushButton工具提示插入一个换行符而不需要我指定它
- VIM Doxygen支持-换行符上自动插入星号(*)