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. QString replace multiple characters in a single call?
Forum Updated to NodeBB v4.3 + New Features

QString replace multiple characters in a single call?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 2.5k 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.
  • S Offline
    S Offline
    SPlatten
    wrote on 5 Aug 2020, 16:36 last edited by
    #1

    I want to replace multiple characters in a QString, I can do this:

    strContent.replace("\n", " ");
    strContent.replace("\r", " ");
    strContent.replace("\t", " ");
    

    What I would like to do is something like this:

    QStringList slstBytes;
    slstBytes << "\n" << "\r" << "\t";
    strContent.replace(slstBytes, " ");
    

    I know this isn't possible with the current build, that's just an example.

    Kind Regards,
    Sy

    R 1 Reply Last reply 5 Aug 2020, 16:48
    0
    • S SPlatten
      5 Aug 2020, 16:36

      I want to replace multiple characters in a QString, I can do this:

      strContent.replace("\n", " ");
      strContent.replace("\r", " ");
      strContent.replace("\t", " ");
      

      What I would like to do is something like this:

      QStringList slstBytes;
      slstBytes << "\n" << "\r" << "\t";
      strContent.replace(slstBytes, " ");
      

      I know this isn't possible with the current build, that's just an example.

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 5 Aug 2020, 16:48 last edited by
      #3

      @SPlatten

      strContent.replace(QRegularExpression("[\\n|\\r|\\t]+"), " ");
      

      --- 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
      3
      • H Online
        H Online
        hskoglund
        wrote on 5 Aug 2020, 16:48 last edited by
        #2

        If it's just those non-printable chars like \n, \r and \t that you want replaced with space, you can use simplified():

        strContent = strContent.simplified();
        
        1 Reply Last reply
        2
        • S SPlatten
          5 Aug 2020, 16:36

          I want to replace multiple characters in a QString, I can do this:

          strContent.replace("\n", " ");
          strContent.replace("\r", " ");
          strContent.replace("\t", " ");
          

          What I would like to do is something like this:

          QStringList slstBytes;
          slstBytes << "\n" << "\r" << "\t";
          strContent.replace(slstBytes, " ");
          

          I know this isn't possible with the current build, that's just an example.

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 5 Aug 2020, 16:48 last edited by
          #3

          @SPlatten

          strContent.replace(QRegularExpression("[\\n|\\r|\\t]+"), " ");
          

          --- 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
          3
          • S Offline
            S Offline
            SPlatten
            wrote on 5 Aug 2020, 16:50 last edited by
            #4

            @raven-worx said in QString replace multiple characters in a single call?:

            strContent.replace(QRegularExpression("[\n|\r|\t]+"), " ");

            Thank you, thats exactly what I was looking for.

            Kind Regards,
            Sy

            1 Reply Last reply
            0

            1/4

            5 Aug 2020, 16:36

            • Login

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