Results 1 to 6 of 6

Thread: String manipulation methods?

  1. #1
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default String manipulation methods?

    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!

  2. #2
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: String manipulation methods?

    The QString Class has all manner of built in functions for doing this kind of thing.

  3. The following user says thank you to JD2000 for this useful post:

    PaladinKnight (15th March 2010)

  4. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: String manipulation methods?

    QString::replace() and friends.

    Read the QString docs.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. The following user says thank you to high_flyer for this useful post:

    PaladinKnight (15th March 2010)

  6. #4
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: String manipulation methods?

    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
    Last edited by PaladinKnight; 16th March 2010 at 04:20.

  7. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: String manipulation methods?

    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?
    Qt Code:
    1. QString esc2 = '\\';
    2. QString esc = "'\'";
    3. test = test.replace(esc2,esc);
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. The following user says thank you to high_flyer for this useful post:

    PaladinKnight (18th March 2010)

  9. #6
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: String manipulation methods?

    (I am not sure what happened but I lost my reply when I try to reply to this message this moring...)

    Hi!

    Quote Originally Posted by high_flyer View Post
    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?
    Qt Code:
    1. QString esc2 = '\\';
    2. QString esc = "'\'";
    3. test = test.replace(esc2,esc);
    To copy to clipboard, switch view to plain text mode 
    What I came up with to do it currently looks something like this:

    Qt Code:
    1. strt.replace(QString("\\\\"), QString("<SOMETHING_THAT_CANT_BE_IN_THE_FILE>"));
    2. strt.replace(QString("\\n"), QString("\n"));
    3. strt.replace(QString("<SOMETHING_THAT_CANT_BE_IN_THE_FILE>"), QString("\\"));
    To copy to clipboard, switch view to plain text mode 

    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

Similar Threads

  1. Accessing QMainWindow methods
    By vieraci in forum Qt Programming
    Replies: 2
    Last Post: 21st April 2009, 08:38
  2. std:string how to change into system:string?
    By yunpeng880 in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2009, 08:51
  3. wrapper of methods
    By mickey in forum General Programming
    Replies: 8
    Last Post: 15th August 2008, 15:33
  4. QComboBox - Few virtual methods
    By gruszczy in forum Qt Programming
    Replies: 17
    Last Post: 16th July 2008, 16:08
  5. QString manipulation - QRegExp
    By mattia in forum Newbie
    Replies: 1
    Last Post: 18th March 2008, 11:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.