10118

Why a comma is omitted at the beginning of a string using QString::right?

Question:

Answering <a href="https://stackoverflow.com/q/52399753/5366641" rel="nofollow">QString replace characters outside of quotes</a> I have stumbled upon a strange behavior.

Executing this code:

foreach (const QString &key, json.keys()) { const QJsonValue &value(json.value(key)); qDebug() << key << value.toString().right(key.length()); }

with this json, <em>A</em>:

QJsonObject json { {"thing1", "thing1value1"}, {"thing2", "thing2value2"}, {"thing3", "thing3value3"} };

and with this json, <em>B</em> (notice the comma after thing2):

QJsonObject json { {"thing1", "thing1value1"}, {"thing2", "thing2,value2"}, {"thing3", "thing3value3"} };

in both cases produces:

"thing1" "value1" "thing2" "value2" "thing3" "value3"

Should not be:

"thing1" "value1" "thing2" ",value2" "thing3" "value3"

in the second case, <em>B</em>?

Answer1:

The <a href="http://doc.qt.io/qt-5/qstring.html#right" rel="nofollow">QString::right</a> function

<blockquote>

Returns a substring that contains the <strong><em>n rightmost</em></strong> characters of the string.

</blockquote>

<sub>[Extra emphasis mine]</sub>

Since the length of the key string is 6, then the 6 rightmost characters will be returned, giving you "value2" without the comma.

If you want to get the string without the leading key you could use <a href="http://doc.qt.io/qt-5/qstring.html#mid" rel="nofollow">mid</a> instead:

value.toString().mid(key.length())

For your use-case I would rather suggest <a href="http://doc.qt.io/qt-5/qstring.html#midRef" rel="nofollow">midRef</a>, since there's no need to actually create a <em>copy</em> of the sub-string.

Recommend

  • Problem writing to a pipe between Fortran and C programs
  • how do you use $.parseJSON in jQuery 1.9.1?
  • Multiple Divs that Stretch to size of window
  • QLocale and QSettings
  • how to get just one scalar from perl split command
  • How to centrally define IComparable on abstract (interface) types in F#
  • Qt iterator not accessing the correct object
  • How to catch keyboard layout change
  • Importing Excel files with a large number of columns header into mysql with c#
  • How to handle empty space in url when downloading image from web?
  • Insert audio album in Android MediaStore
  • XBee Linux Serial Port on Rasberry Pi
  • Smack 4.1.0 android Roster not displaying
  • Send data from edittext to listview
  • Retrofit SocketTimeoutException (and/or http 500 error) on http-POST
  • Unable to connect to OnVif enabled camera using C#
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • Retrieve IP address of device
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • Array with custom indexes in Ionic2
  • Play WS (2.2.1): post/put large request
  • ilmerge with a PFX file
  • Why value captured by reference in lambda is broken? [duplicate]
  • Different response to non-authenticated users and AJAX calls
  • Volley JsonObjectRequest send headers in GET Request
  • Check if a string to interpolate provides expected placeholders
  • Javascript convert timezone issue
  • Javascript + PHP Encryption with pidCrypt
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Why joiner is not used after Sequence generator or Update statergy
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • Is it possible to post an object from jquery to bottle.py?
  • Recursive/Hierarchical Query Using Postgres
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal