Results 1 to 6 of 6

Thread: Remove all spaces in string except spaces in quotes

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    1
    Qt products
    Qt5

    Default Re: Remove all spaces in string except spaces in quotes

    Quote Originally Posted by Alundra View Post
    QString has what you want : http://qt-project.org/doc/qt-5/QString.html
    You have to use Right, Left, IndexOf and LastIndexOf

    Is there an easier way to do this, because potentially there could be many strings in it that I don't want the spaces removed from. I'm not even sure how to approach this.

    Like:

    Qt Code:
    1. @BEGIN:4; 17, 1;1, "This is some text right here."; 3; 18;1, "This is more text."; 3; 18;1, "Another set of text right here."; 3; 2; 5;11, LABEL;end;@LABEL:7, 1;8;end;@END
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Remove all spaces in string except spaces in quotes

    There are several ways to approach this.

    One way is to "parse" the input.You walk through the string in a loop.
    If the loop body encouters a quote it toggles a quote flag. If it encounters a space and the quote flag is not on, it removes the space.

    Another way, as Alundra, suggested, is to search for the quotes.
    You search for the first quote starting at position 0. You can then use replace to remove all space from the string before that. Then you search for the next quote using the other's position +1 as a start. You append that substring directly into the result. Then you repeat with the remaining input string.

    Cheers,
    _

  3. #3
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Remove all spaces in string except spaces in quotes

    Quote Originally Posted by bnosam View Post
    Is there an easier way to do this, because potentially there could be many strings in it that I don't want the spaces removed from. I'm not even sure how to approach this.

    Like:

    Qt Code:
    1. @BEGIN:4; 17, 1;1, "This is some text right here."; 3; 18;1, "This is more text."; 3; 18;1, "Another set of text right here."; 3; 2; 5;11, LABEL;end;@LABEL:7, 1;8;end;@END
    To copy to clipboard, switch view to plain text mode 
    Here is one more version that uses regular expressions and removes the kludge used to skip over escaped quotes (the regex doesn't match on escaped quotes):

    Qt Code:
    1. // remove spaces in string outside of quotes
    2.  
    3. QString str = "@BEGIN:4; 17, 1;1, \"This is some text right here.\"; 3; 18;1, \"This is more text.\"; 3; 18;1, \"Another set of text right here.\"; 3; 2; 5;11, LABEL;end;@LABEL:7, 1;8;end;@END";
    4. QString outstr;
    5.  
    6. qDebug("....................................................................................................1.........1.........1.........1.........1.........1.........1.........1.........");
    7. qDebug("..........1.........2.........3.........4.........5.........6.........7.........8.........9.........0.........1.........2.........3.........4.........5.........6.........7.........");
    8. qDebug("012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789");
    9. qDebug("%s", qPrintable(str));
    10.  
    11. int pos, q1, q2 = 0;
    12.  
    13. QRegularExpression re("(?<![\\\\])\"");
    14.  
    15. pos = 0;
    16. q1 = str.indexOf(re);
    17.  
    18. if (q1 == -1)
    19. {
    20. // no quotes in string, so just remove all spaces
    21. outstr += str.mid(0).remove(" ");
    22. }
    23. else
    24. {
    25. while (q1 != -1 && q2 != -1)
    26. {
    27. q2 = str.indexOf(re, q1+1);
    28. if (q2 == -1)
    29. {
    30. // unbalanced quotes, so strip all spaces from current pos to end of string (or return an error, etc)
    31. outstr += str.mid(pos).remove(" ");
    32. }
    33. else
    34. {
    35. // found balanced quote, so strip spaces before quote and append
    36. // quoted portion, then look for next quoted section and continue looping
    37. outstr += str.mid(pos, q1-pos).remove(" ");
    38. outstr += str.mid(q1,q2-q1+1);
    39. pos = q2 + 1;
    40. q1 = str.indexOf(re, pos);
    41. if (q1 == -1)
    42. {
    43. // no more quoted text, remove spaces from rest of string
    44. outstr += str.mid(pos).remove(" ");
    45. }
    46. }
    47. }
    48. }
    49.  
    50. qDebug("%s", qPrintable(outstr));
    To copy to clipboard, switch view to plain text mode 

    And the output that shows the original string and the output string after removing spaces (with a header to show column offset):

    Qt Code:
    1. ....................................................................................................1.........1.........1.........1.........1.........1.........1.........1.........
    2. ..........1.........2.........3.........4.........5.........6.........7.........8.........9.........0.........1.........2.........3.........4.........5.........6.........7.........
    3. 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
    4. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    5. @BEGIN:4; 17, 1;1, "This is some text right here."; 3; 18;1, "This is more text."; 3; 18;1, "Another set of text right here."; 3; 2; 5;11, LABEL;end;@LABEL:7, 1;8;end;@END
    6. @BEGIN:4;17,1;1,"This is some text right here.";3;18;1,"This is more text.";3;18;1,"Another set of text right here.";3;2;5;11,LABEL;end;@LABEL:7,1;8;end;@END
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 4
    Last Post: 30th August 2020, 19:44
  2. Replies: 3
    Last Post: 17th April 2012, 16:05
  3. Spaces in QTextEdit
    By jgrauman in forum Qt Programming
    Replies: 2
    Last Post: 22nd June 2009, 18:28
  4. Spaces between lines in QTextEdit
    By troorl_ua in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2007, 00:06
  5. qmake INCLUDEPATH with spaces
    By bitChanger in forum Qt Programming
    Replies: 8
    Last Post: 28th April 2006, 05:39

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
  •  
Qt is a trademark of The Qt Company.