// Example to remove spaces from string except when properly quoted
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";
int pos = 0;
int q1 = str.indexOf("\"");
// skip escaped quotes
while (q1 != -1 && str.mid(q1-1,2) == "\\\"")
q1 = str.indexOf("\"", q1+1);
if (q1 == -1)
{
// no quotes in string, so just remove all spaces
outstr += str.mid(0).remove(" ");
}
else
{
while (q1 != -1)
{
int q2 = str.indexOf("\"", q1+1);
// skip escaped quotes
while (q2 != -1 && str.mid(q2-1,2) == "\\\"")
q2 = str.indexOf("\"", q2+1);
if (q2 == -1)
{
// unbalanced quotes, so strip all spaces from current pos to end of string (or return an error, etc)
outstr += str.mid(pos).remove(" ");
q1 = -1; // cause loop to break because unbalanced quote
}
else
{
// found balanced quote, so strip spaces before quote and append
// quoted portion, then look for next quoted section and continue looping
outstr += str.mid(pos, q1-pos).remove(" ");
outstr += str.mid(q1,q2-q1+1);
pos = q2 + 1;
q1 = str.indexOf("\"", pos);
// skip escaped quotes
while (q1 != -1 && str.mid(q1-1,2) == "\\\"")
q1 = str.indexOf("\"", q1+1);
if (q1 == -1)
{
// no more quoted text, remove spaces from rest of string
outstr += str.mid(pos).remove(" ");
}
}
}
}
qDebug(" str='%s'", qPrintable(str));
qDebug("outstr='%s'", qPrintable(outstr));
// Example to remove spaces from string except when properly quoted
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";
QString outstr;
int pos = 0;
int q1 = str.indexOf("\"");
// skip escaped quotes
while (q1 != -1 && str.mid(q1-1,2) == "\\\"")
q1 = str.indexOf("\"", q1+1);
if (q1 == -1)
{
// no quotes in string, so just remove all spaces
outstr += str.mid(0).remove(" ");
}
else
{
while (q1 != -1)
{
int q2 = str.indexOf("\"", q1+1);
// skip escaped quotes
while (q2 != -1 && str.mid(q2-1,2) == "\\\"")
q2 = str.indexOf("\"", q2+1);
if (q2 == -1)
{
// unbalanced quotes, so strip all spaces from current pos to end of string (or return an error, etc)
outstr += str.mid(pos).remove(" ");
q1 = -1; // cause loop to break because unbalanced quote
}
else
{
// found balanced quote, so strip spaces before quote and append
// quoted portion, then look for next quoted section and continue looping
outstr += str.mid(pos, q1-pos).remove(" ");
outstr += str.mid(q1,q2-q1+1);
pos = q2 + 1;
q1 = str.indexOf("\"", pos);
// skip escaped quotes
while (q1 != -1 && str.mid(q1-1,2) == "\\\"")
q1 = str.indexOf("\"", q1+1);
if (q1 == -1)
{
// no more quoted text, remove spaces from rest of string
outstr += str.mid(pos).remove(" ");
}
}
}
}
qDebug(" str='%s'", qPrintable(str));
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.
// input string
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'
// output string
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'
// input string
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'
// output string
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
Bookmarks