Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Creates a single QString from QStringList + join or by using QString::operator+=
Forum Updated to NodeBB v4.3 + New Features

Creates a single QString from QStringList + join or by using QString::operator+=

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 3.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mistralegna
    wrote on 15 Jul 2016, 08:47 last edited by
    #1

    Hello,

    I have to build a QString from several strings. I was wondering :

    "Is it better to use the provided operator+= in QString or to use QStringList to complete it and then join the result at the end ?"

    Thank you by advance

    K 1 Reply Last reply 15 Jul 2016, 08:51
    0
    • M mistralegna
      15 Jul 2016, 08:47

      Hello,

      I have to build a QString from several strings. I was wondering :

      "Is it better to use the provided operator+= in QString or to use QStringList to complete it and then join the result at the end ?"

      Thank you by advance

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 15 Jul 2016, 08:51 last edited by
      #2

      @mistralegna
      Is this a bottleneck for your application's performance? If not, my advice is use whatever is more convenient (I usually prefer QString::join if I have the strings in a list already).

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • N Offline
        N Offline
        NickV
        wrote on 15 Jul 2016, 09:00 last edited by
        #3

        You can insert all your strings into QStringList and then use join to get a single string. Inside join you can set delimiter.

        For example:

        StringList temp;
        temp.append("a");
        temp.append("b");
        temp.append("c");
        String new_string = temp.join("@");

        The result will be: a@b@c

        1 Reply Last reply
        1
        • M Offline
          M Offline
          mistralegna
          wrote on 15 Jul 2016, 09:42 last edited by
          #4

          Yes, I am aware of this possibility. The main question is: what are the performance of each way to doing this ?

          Thank you for your answers!

          J R 2 Replies Last reply 15 Jul 2016, 09:55
          0
          • N Offline
            N Offline
            NickV
            wrote on 15 Jul 2016, 09:46 last edited by
            #5

            I believe that join is O(1). With += you should have loop if there are many strings to concat.

            1 Reply Last reply
            0
            • M mistralegna
              15 Jul 2016, 09:42

              Yes, I am aware of this possibility. The main question is: what are the performance of each way to doing this ?

              Thank you for your answers!

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 15 Jul 2016, 09:55 last edited by
              #6

              @mistralegna You can write a simple benchmark to check what is faster in your case.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • M mistralegna
                15 Jul 2016, 09:42

                Yes, I am aware of this possibility. The main question is: what are the performance of each way to doing this ?

                Thank you for your answers!

                R Offline
                R Offline
                raven-worx
                Moderators
                wrote on 15 Jul 2016, 10:11 last edited by
                #7

                @mistralegna
                Basically both methods should be the same, since you need to create a single string from a list of strings.
                Still i would prefer QStringList::join() since (AFAIK) it should use QStringBuilder internally. The big advantage is that memory allocations are reduced to a minimum which results in a performance gain.

                Note: you can also use the (internal) StringBuilder by using the % operator instead the + operator.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0

                1/7

                15 Jul 2016, 08:47

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved