PDA

View Full Version : String manipulation methods?



PaladinKnight
15th March 2010, 11:30
Hi!

I want to modify a program that is using Qt and I am having difficulties finding the kind of method I'm looking for.

Essentially I want to find a method that remaps \\ to \, \n to a newline, etc...

Is there such a thing in Qt and if so where could I find more info on such string manipulations methods?

Thank you!

JD2000
15th March 2010, 11:41
The QString Class has all manner of built in functions for doing this kind of thing.

high_flyer
15th March 2010, 11:41
QString::replace() and friends.

Read the QString docs.

PaladinKnight
15th March 2010, 23:04
Hi!

Thank you for your help!

I guess I should have given more info...

I am already using QString:replace but the problem I have with it is that while it's pretty easy to map \n to a newline, dealing \\ which remaps to \ is proving to be problematic.

I can't simply map \n to a newline and \\ to a \ by using replace two times (regardless of the order in which I do them) as it won't give the right results.

I have tried to do it with a regex (using QRegExp) but I seem to have a problem with the back references...

I am kind of looking for something like Qt::escape but in reverse and with a different remapping. Instead of mapping characters their HTML metacharacters, I need something which transform the usual C escape sequences back to the characters they represent...

However I think I might be able to achieve what I want by doing (at least) three passes with .replace (I'll have to make sure my idea works) but if there is something that already does what I want to do I would prefer to use it.

Thank you!

Nick

high_flyer
16th March 2010, 09:29
I can't simply map \n to a newline
Do you mean \n to "newline" or to carriage return - '\r' or something else?
Since \n IS newline.


I can't simply map \n to a newline and \\ to a \ by using replace two times (regardless of the order in which I do them) as it won't give the right results.
Does this do what you want?


QString esc2 = '\\';
QString esc = "'\'";
test = test.replace(esc2,esc);

PaladinKnight
16th March 2010, 23:44
(I am not sure what happened but I lost my reply when I try to reply to this message this moring...)

Hi!


Do you mean \n to "newline" or to carriage return - '\r' or something else?
Since \n IS newline.

Not when it's actually a backslash followed by a "n"... I want to convert strings I read from a file so that when I see a backslash followed by a "n" I convert it to a newline and if I see a backslash followed by a backslash I convert that to a single backslash.



Does this do what you want?


QString esc2 = '\\';
QString esc = "'\'";
test = test.replace(esc2,esc);


What I came up with to do it currently looks something like this:



strt.replace(QString("\\\\"), QString("<SOMETHING_THAT_CANT_BE_IN_THE_FILE>"));
strt.replace(QString("\\n"), QString("\n"));
strt.replace(QString("<SOMETHING_THAT_CANT_BE_IN_THE_FILE>"), QString("\\"));


Because of restrictions imposed by the format of the file I read this from it's actually pretty easy to find to replace <SOMETHING_THAT_CANT_BE_IN_THE_FILE> with.

I have read that though I could actually directly pass the strings to replace() instead of the QString it's actually preferrable to use QString there, is that right?

If I had to do more than these two remapings though I guess I would have eventually have no choice but to decode the string character by character and to rebuild it though otherwise it would eventually be too slow to do this in multiple passes...

I was hoping that there would be something in Qt that does that when I saw Qt::escape... I could have sworn I read about something that did it (something like unbackslash?) but I can't seem to find where I found it and it's not unbackslash apparently...

Thank you!

Nick