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. Replace an element in a QString
Forum Updated to NodeBB v4.3 + New Features

Replace an element in a QString

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 681 Views 1 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.
  • D Offline
    D Offline
    developer_96
    wrote on last edited by
    #1

    Hello,
    i have following problem:
    I saved in a QString an XMLStart.

    QString f;
    

    QString f:

    <class="go">
    <number>2</number>
    <column>3</column>
    <row>4</column>
    

    I want to change for example the value of row with 7 in this String by for example using replace or something like that.

    f.replace();
    

    How can i achive that ? Is that possible?

    Regards

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      yes its possible if you are careful

      https://doc.qt.io/qt-5/qstring.html#replace-5

      f.replace("row>4", "row>7")

      or anything that will not replace other strings. so the trick is to see if there is something you can find it with.
      if there are multi that matches all will be changed.

      If not, then you have to parse the XML and do it more structured.

      1 Reply Last reply
      2
      • D Offline
        D Offline
        developer_96
        wrote on last edited by
        #3

        @mrjj thank you. And how it would look if i want to put a variable instead of 7 in replace? how would be syntax for that?

        jsulmJ 1 Reply Last reply
        0
        • D developer_96

          @mrjj thank you. And how it would look if i want to put a variable instead of 7 in replace? how would be syntax for that?

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @developer_96

          f.replace("row>4", QString("row>%1").arg(variable));
          

          https://doc.qt.io/qt-5/qstring.html#arg

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

          1 Reply Last reply
          4
          • D Offline
            D Offline
            developer_96
            wrote on last edited by
            #5

            thank you @jsulm.

            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