2014-09-25 5 views
0

특정 줄의 텍스트를 바꾸려고하는데 성공하지 못했습니다.특정 줄의 텍스트 바꾸기

hello 
my 
friend! 

텍스트에 라인이 교체 :

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)); 

답변

2

난 당신이 줄 바꿈 처리와 같은 뭔가를 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!" 
+0

감사합니다! 잘 작동합니다. – Niunzin

관련 문제