![Quote](images/misc/quote_icon.png)
Originally Posted by
hackerNovitiate
Hmm... to clarify, can you please check if the following are correct?
1) remove() modifies the original string, and returns the same string
Yes.
![Quote](images/misc/quote_icon.png)
Originally Posted by
hackerNovitiate
2) simplified() leaves the original string untouched, and returns a modified copy
Correct
![Quote](images/misc/quote_icon.png)
Originally Posted by
hackerNovitiate
3) A function that returns "QString" will return a shallow copy of the string, so this CAN used to return a local variable
Correct
![Quote](images/misc/quote_icon.png)
Originally Posted by
hackerNovitiate
4) A function that returns "QString&" will return a reference to an existing string, so this MUST be used to return a local variable
Actually, you might run into trouble when referencing a local variable that is returned from a string. The variable will be destructed when the function ends, leaving the reference referencing a variable that doesn't exist anymore (which shouldn't happen). You return a reference when you want the calling function to be able to edit the value in place (in a QList for example).
strings << "string1" << "string2";
strings[0].replace('1','0');
QStringList strings;
strings << "string1" << "string2";
strings[0].replace('1','0');
To copy to clipboard, switch view to plain text mode
This edits the string at location 0 to change every 1 into a 0.
QString::remove() returns a reference to itself, so you can do the following:
txt.remove(8, 1).remove(5, 2);
// txt is now "some x"
QString txt = "some text";
txt.remove(8, 1).remove(5, 2);
// txt is now "some x"
To copy to clipboard, switch view to plain text mode
Bookmarks