Results 1 to 6 of 6

Thread: Remove all spaces in string except spaces in quotes

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

    Default Remove all spaces in string except spaces in quotes

    Lets say I have a string like this:

    Qt Code:
    1. @BEGIN:4; 17, 1;1, "This is an example of text."; 3; 2; 5;11, LABEL;end;@LABEL:7, 1;8;end;@END
    To copy to clipboard, switch view to plain text mode 

    What would be an easy way to remove all whitespace except in the part between the quotes ("This is an example of text.")?

  2. #2
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

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

    QString has what you want : http://qt-project.org/doc/qt-5/QString.html
    You have to use Right, Left, IndexOf and LastIndexOf

  3. #3
    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 

  4. #4
    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,
    _

  5. #5
    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
    Lets say I have a string like this:

    Qt Code:
    1. @BEGIN:4; 17, 1;1, "This is an example of text."; 3; 2; 5;11, LABEL;end;@LABEL:7, 1;8;end;@END
    To copy to clipboard, switch view to plain text mode 

    What would be an easy way to remove all whitespace except in the part between the quotes ("This is an example of text.")?
    Not sure how elegant the following code is, but seems to work for the example string you gave, as well as other test strings I used:
    Qt Code:
    1. // Example to remove spaces from string except when properly quoted
    2. 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";
    3. QString outstr;
    4.  
    5. int pos = 0;
    6. int q1 = str.indexOf("\"");
    7. // skip escaped quotes
    8. while (q1 != -1 && str.mid(q1-1,2) == "\\\"")
    9. q1 = str.indexOf("\"", q1+1);
    10.  
    11. if (q1 == -1)
    12. {
    13. // no quotes in string, so just remove all spaces
    14. outstr += str.mid(0).remove(" ");
    15. }
    16. else
    17. {
    18. while (q1 != -1)
    19. {
    20. int q2 = str.indexOf("\"", q1+1);
    21. // skip escaped quotes
    22. while (q2 != -1 && str.mid(q2-1,2) == "\\\"")
    23. q2 = str.indexOf("\"", q2+1);
    24. if (q2 == -1)
    25. {
    26. // unbalanced quotes, so strip all spaces from current pos to end of string (or return an error, etc)
    27. outstr += str.mid(pos).remove(" ");
    28. q1 = -1; // cause loop to break because unbalanced quote
    29. }
    30. else
    31. {
    32. // found balanced quote, so strip spaces before quote and append
    33. // quoted portion, then look for next quoted section and continue looping
    34. outstr += str.mid(pos, q1-pos).remove(" ");
    35. outstr += str.mid(q1,q2-q1+1);
    36. pos = q2 + 1;
    37. q1 = str.indexOf("\"", pos);
    38. // skip escaped quotes
    39. while (q1 != -1 && str.mid(q1-1,2) == "\\\"")
    40. q1 = str.indexOf("\"", q1+1);
    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(" str='%s'", qPrintable(str));
    51. qDebug("outstr='%s'", qPrintable(outstr));
    To copy to clipboard, switch view to plain text mode 
    For the case where the code encounters unbalanced quotes, I simply treat the portion of the string at the current position before/after the quote as not being quoted, which seems reasonable since it's not properly quoted. For your use case, you may want to return an error value, etc.

    Output that shows the input string and the output string:
    Qt Code:
    1. // input string
    2. 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'
    3. // output string
    4. outstr='@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 

    Hope that helps,

    Jeff
    Last edited by jefftee; 24th June 2014 at 08:51. Reason: Added code to ignore escaped quotes in string (i.e. \")

  6. #6
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.