我试着替换某一行的文字,但没有成功。(我搜了很多遍,但什么也没找到)
类似于:
hello
my
friend!将第2行改为某些案文:
hello
AEEEHO NEW LINE TEXT
friend!我创建了一个QStringList,并尝试逐行读取文本并通过更改行添加到这个列表中,但没有成功。
int line = 1; // to change the second line
QString newline = "my new text";
QStringList temp;
int i = 0;
foreach(QString curlineSTR, internalCode.split('\n'))
{
if(line == i)
temp << newline;
else
temp << curlineSTR;
i++;
}
internalCode = "";
foreach(QString txt, temp)
internalCode.append(QString("%1\n").arg(txt));发布于 2014-09-25 03:51:26
我相信您正在寻找QRegExp来处理换行符,并执行如下操作:
QString internalcode = "hello\nmy\nfriend!";
int line = 1; // to change the second line
QString newline = "another text";
// Split by newline command
QStringList temp = internalcode.split(QRegExp("\n|\r\n|\r"));
internalcode.clear();
for (int i = 0; i < temp.size(); i++)
{
if (line == i)
internalcode.append(QString("%0\n").arg(newline));
else
internalcode.append(QString("%0\n").arg(temp.at(i)));
}
//Use this to remove the last newline command
internalcode = internalcode.trimmed();
qDebug() << internalcode;以及产出:
"hello
another text
friend!"https://stackoverflow.com/questions/26028603
复制相似问题