Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个带有多行文本的 QString,开头没有空格,例如:
Lorem ispum Dolor a si met Hulu il it er
我想为每一行添加空间以获得如下内容:
有关信息,我使用 QT 的 QString
你可以使用QString::replace():
QString::replace()
QString s = "Lorem ispum\nDolor a si met\nHulu il it er "; s.replace(QRegExp("^"), "\t");
您也可以不使用正则表达式:
s.insert(0, '\t'); s.replace('\n', "\n\t");
这将\t在每一行的开头添加一个制表符 ( ),如果要添加空格,只需替换\t为空格即可。
\t
只需遍历字符串中的每个字符,同时复制到第二个字符串。一旦你看到一个换行符然后复制它并添加所需的空格。
或者简单地使用以下replace功能:
replace
str.replace('\n', "\n\t");