Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove backslashes from QString?

Tags:

c++

qt

qstring

qt5

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.

like image 292
Francesco Pegoraro Avatar asked Dec 08 '25 06:12

Francesco Pegoraro


2 Answers

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.

like image 55
Nikos C. Avatar answered Dec 09 '25 18:12

Nikos C.


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

like image 29
M4HdYaR Avatar answered Dec 09 '25 19:12

M4HdYaR