How to remove backslashes from QString? - c++

Using QNetworkManager get method I am receiving a json from a url.
Doing: qDebug()<<(QString)reply->readAll(); the result is:
"\r\n[{\"id\":\"1\",\"name\":\"Jhon\",\"surname\":\"Snow\",\"phone\":\"358358358\"}]"
So I am doing strReply = strReply.simplified(); , and the result is:
"[{\"id\":\"1\",\"name\":\"Jhon\",\"surname\":\"Snow\",\"phone\":\"358358358\"}]"
But I can't use that to parse it like a Json to use it in my qt program.
So I think I need to remove every backslashes \ and obtain:
"[{"id":"1","name":"Jhon","surname":"Snow","phone":"348348348"}]"
I tried strReply.remove(QRegExp( "\\\" ) ); but any odd concatenation of \ is causing the interpreter to think at every thing that comes after the last \ as a string.

You're probably running into qDebug's feature that escapes quotes and newlines. Your string most probably doesn't actually have any backslashes in it.
When you're trying to print a string using qDebug(), you need to use qDebug().noquote() if you don't want qDebug() to artificially insert backslashes in the output.
So your string should be fine. It doesn't have any backslashes in it at all.

As described in the documentation You can remove a character with remove function
QString t = "Ali Baba";
t.remove(QChar('a'), Qt::CaseInsensitive);
// Will result "li Bb"
You can put '\\' instead of 'a' to remove your backslashes from your QString

Related

Odd substitution behaviour in perl substitution of rtf file

I am trying to use the perl module "RTF::Writer" for strings of text that must be a mix of formats. This is proving more complicated than I anticipated. I am just trying a test at the moment with:
$rtf->paragraph( \'\b', "Name: $name, le\cf1 ng\cf0 th $len" );
but this writes:
{\pard
\b
Name: my_name, le\'061 ng\'060 th 7
\par}
where \'061 should be \cf1 and \'060 should be \cf0.
I then tried to remedy this with a perl 1-liner:
perl -pi -e "s/\'06/\cf/g"
but this made things worse, I do not know what "\^F" represents in vi, but that is what it shows.
It did not matter if I escaped the backslashes or not.
Can anyone explain this behavior, and what to do about it?
Can anyone suggest how to get the RTF::Writer to create the file as desired from the start?
Thanks
\ is a special character in double-quoted string literals. If you want a string that contains \, you need to use \\ in the literal. To create the string \cf1, you need to use "\\cf1". ("\cf" means Ctrl-F, which is to say the byte 06.)
Alternatively, \ is only special if followed by \ or a delimiter in single-quoted string literals. So the string \cf1 could also be created from '\cf1'.
Both produce the string you want, but they don't produce the document you want. That's because there's a second problem.
When you pass a string to RTF::Writer, it's expected to be text to render. But you are passing a string you wanted included as is in the final document. You need to pass a reference to a string if you want to provide raw RTF. \'...', \"..." and \$str all produce a reference to a string.
Fixed:
use RTF::Writer qw( );
my $name = "my_name";
my $rtf = RTF::Writer->new_to_file("greetings.rtf");
$rtf->prolog( 'title' => "Greetings, hyoomon" );
$rtf->paragraph( \'\b', "Name: $name, le", \'\cf1', "ng", \'\cf0', "th".length($name));
$rtf->close;
Output from the call to paragraph:
{\pard
\b
Name: my_name, le\cf1
ng\cf0
th7
\par}
Note that I didn't use the following because it would be code injection bug:
$rtf->paragraph(\("\\b Name: $name, le\\cf1 ng\\cf0 th".length($name)));
Don't pass text such as the contents of $name using \...; use that for raw RTF only.

Delete everything before a double quote

I'm trying to clean a CSV file which has a column with contents like this:
Sometexthere1", "code"=>"47.51-2-01"}]
And I would like to remove everything before the first quote (") in order to keep just this:
Sometexthere1
I know that I can use $` to get everything before some match in regex, but I am not understanding how to keep just the string before the first double quote.
Parameter expansion does this well enough:
# Define a variable
s='Sometexthere1", "code"=>"47.51-2-01"}]'
# expand it, removing the longest possible match (from the end) for '"'*
result=${s%%'"'*}
# demonstrate that result by printing it
printf '%s\n' "$result"
...properly returns Sometexthere1.
You probably mean "delete everything after a double quote"? In Open Refine, you can use this GREL formula :
value.replace(/".+/, "")
> Result : Sometexthere1

How to strip a certain string from each the list items

I have list of file paths inside C:\ stored in a list called filepaths, now i have to strip off C:\ from all the filepaths while doing a for loop.
I'm not able to find a strip method while looping, as each element is coming as Type list. Please find my code below.
filepaths = ['C:\folder\file1.jpg','C:\file2.png','C:\file3.xls']
tobestriped = 'C:\'
for filepath in filepaths:
newfilepath = filepath.strip(tobestriped)
print(newfilepath)
Well, to begin with, in tobestriped you'll get an error as \' will be escaped. You can use tobestriped = 'C:\\'.
From this SO answer:
A "raw string literal" is a slightly different syntax for a string
literal, in which a backslash, \, is taken as meaning "just a
backslash" (except when it comes right before a quote that would
otherwise terminate the literal) -- no "escape sequences" to represent
newlines, tabs, backspaces, form-feeds, and so on. In normal string
literals, each backslash must be doubled up to avoid being taken as
the start of an escape sequence.
Next, In your paths list \f will also be escaped. To get rid of this issue, make those strings raw strings:
filepaths = [r'C:\folder\file1.jpg', r'C:\file2.png', r'C:\file3.xls']
An you will have the desired result:
filepaths = [r'C:\folder\file1.jpg', r'C:\file2.png', r'C:\file3.xls']
tobestriped = 'C:\\'
for filepath in filepaths:
newfilepath = filepath.strip(tobestriped)
print(newfilepath)
Output:
folder\file1.jpg
file2.png
file3.xls
An alternative to your solution would be to take advantage of the fact that all your strings begin with C:\ so you can do something like this:
print([x[3:] for x in filepaths])

JSONCPP is adding extra double quotes to string

I have a root in JSONcpp having string value like this.
Json::Value root;
std::string val = "{\"stringval\": \"mystring\"}";
Json::Reader reader;
bool parsingpassed = reader.parse(val, root, false);
Now when I am trying to retrieve this value using this piece of code.
Json::StreamWriterBuilder builder;
builder.settings_["indentation"] = "";
std::string out = Json::writeString(builder, root["stringval"]);
here out string ideally should be giving containing:
"mystring"
whereas it is giving output like this:
"\"mystring\"" \\you see this in debug mode if you check your string content
by the way if you print this value using stdout it will be printed something like this::
"mystring" \\ because \" is an escape sequence and prints " in stdout
it should be printing like this in stdout:
mystring \\Expected output
Any idea how to avoid this kind of output when converting json output to std::string ?
Please avoid suggesting fastwriter as it also adds newline character and it deprecated API as well.
Constraint: I do not want to modify the string by removing extra \" with string manipulation rather I am willing to know how I can I do that with JSONcpp directly.
This is StreamWriterBuilder Reference code which I have used
Also found this solution, which gives optimal solution to remove extra quotes from your current string , but I don't want it to be there in first place
I had this problem also until I realized you have to use the Json::Value class accessor functions, e.g. root["stringval"] will be "mystring", but root["stringval"].asString() will be mystring.
Okay so This question did not get answer after thorough explanation as well and I had to go through JSONCPP apis and documentation for a while.
I did not find any api as of now which takes care of this scenario of extra double quote addition.
Now from their wikibook I could figure out that some escape sequences might come in String. It is as designed and they haven't mentioned exact scenario.
\" - quote
\\ - backslash
\/ - slash
\n - newline
\t - tabulation
\r - carriage return
\b - backspace
\f - form feed
\uxxxx , where x is a hexadecimal digit - any 2-byte symbol
Link Explaining what all extra Escape Sequence might come in String
Anyone coming around this if finds out better explanation for the same issue , please feel free to post your answer.Till then I guess only string manipulation is the option to remove those extra escape sequence..

ASCII Art, Issue with Code Blocks C++

I can't understand why the "\" doesn't appear when i run the program. I want to make some ASCII Art and "\" is basic for the picture i want to make.Is there any solution? I am using Code Blocks .
With C++2011 you can use raw string literals, e.g.:
std::cout << R"(\)" << '\n';
The sequence R"( starts the string and )" ends the string. If the string )" needs to be embedded into the string, you can add some string between the " and the ( which then needs to be repeated between the ) and the " to end the string.
Of course, it may just be simpler to escape the escape character and to use \\ as you already mentioned.
You have to use 2 \ since the \ character is known as an escape key, like if you want to go to the next line you have to use \n and that lets C++ know that you want to move to the next line, so every time you use the \ character, you have to type it like \
I've found it. You have to enter 2 times the "\" and then it will appear.