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.
  • mistralegnaM Offline
    mistralegnaM Offline
    mistralegna
    wrote on 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

    kshegunovK 1 Reply Last reply
    0
    • mistralegnaM mistralegna

      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

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on 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
      • NickVN Offline
        NickVN Offline
        NickV
        wrote on 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
        • mistralegnaM Offline
          mistralegnaM Offline
          mistralegna
          wrote on 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!

          jsulmJ raven-worxR 2 Replies Last reply
          0
          • NickVN Offline
            NickVN Offline
            NickV
            wrote on 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
            • mistralegnaM mistralegna

              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!

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 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
              • mistralegnaM mistralegna

                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!

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on 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

                • Login

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