damio
damio

Reputation: 6301

Adding characters to each line in a QString

I have a QString with a multi-line text without space at the begining like:

Lorem ispum   
Dolor a si met   
Hulu il it er   

and I would like to add space to each line to obtain something like this:

      Lorem ispum  
      Dolor a si met   
      Hulu il it er     

For information, I use QString of QT

Upvotes: 0

Views: 5580

Answers (2)

zakinster
zakinster

Reputation: 10688

You could use QString::replace() :

QString s = "Lorem ispum\nDolor a si met\nHulu il it er ";
s.replace(QRegExp("^"), "\t");

You could also do it without regular expression :

s.insert(0, '\t');
s.replace('\n', "\n\t");

This would add one tab (\t) at the beginning of each line, if you want to add spaces, just replace \t with spaces.

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409176

Just iterate over every character in the string, while copying to a second string. As soon as you see a newline then copy that and add the spaces needed.

Or simply use the replace function:

str.replace('\n', "\n\t");

Upvotes: 1

Related Questions