PDA

View Full Version : QString - find and replace text



graciano
24th January 2009, 18:09
Hi!
Imagine i want to replace all occurrences of "textA" by "textB" in "str".
(QString str, textA, textB; )
Is there a function for this or i must create a loop and replace until no more occurrences found?

Thanks

jpn
24th January 2009, 18:35
Take a look at QString::replace() docs and read what it does.

aamer4yu
24th January 2009, 18:35
Didnt you have a look at QString::replace ??

graciano
24th January 2009, 21:35
Yeap ... missed that one.
Thanks

PS: Promisse i will look carefully before posting nest time :o

QString & QString::replace ( const QString & before, const QString & after, Qt::CaseSensitivity cs = Qt::CaseSensitive )

This is an overloaded member function, provided for convenience.

Replaces every occurrence of the string before with the string after.

If cs is Qt::CaseSensitive (the default), the search is case sensitive; otherwise the search is case insensitive.

Example:

QString str = "colour behaviour flavour neighbour";
str.replace(QString("ou"), QString("o"));
// str == "color behavior flavor neighbor"

Note: The replacement text is not rescanned after it is inserted.

Example:

QString equis = "xxxxxx";
equis.replace("xx", "x");
// equis == "xxx"