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. How to replace parts of a string using a correlation table?
Forum Updated to NodeBB v4.3 + New Features

How to replace parts of a string using a correlation table?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 428 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.
  • U Offline
    U Offline
    Urbi
    wrote on 18 Feb 2022, 10:40 last edited by
    #1

    I want to add some new features to a quite complex Qt project that can code and decode certain messages. For that, I need to find a way to automatically replace parts of a string according to a correlation table. The correlation table shall be read from a file one time after starting the program, but then the data must be stored in RAM or similar so that the replacement is fast enough. Goal is to automatically replace any ".00", ".01", ... ,".99" (= maximal 100 values) of a given string by the values specified in the correlation table (and vice versa, when coding the string).

    Example:
    Content of the correlation table is: ABCD,GT,KF,H,CVP
    This shall stand for .00,.01,.02,.03,.04,...,.99 (= maximal 100 values)
    The correlation table shall be read after program start (or after a command) from e.g c:\table\table.dat, but data shall then come from RAM or similar.

    Decoder:
    Content of a given string which I read: gf46jkgj ghgo r .01
    Content of that string after automatical replacement: gf46jkgj ghgo r GT (= .01 replaced by GT)
    Content of a given string: gf46jkgj ghgo r .04
    Content of that string after automatical replacement: gf46jkgj ghgo r CVP (= .04 replaced by CVP)

    Coder:
    Content of a given value KF
    Content of the generated message before replacement: gf46jkgj ghgo r KF
    Content of the generated message after replacement: gf46jkgj ghgo r .02 (= KF replaced by .02)

    How I can I program something like that? (My C++ programming skills are not the best. Please be so kind and explain it well enough or at best give me a concrete example)

    J 1 Reply Last reply 18 Feb 2022, 11:53
    0
    • U Urbi
      18 Feb 2022, 10:40

      I want to add some new features to a quite complex Qt project that can code and decode certain messages. For that, I need to find a way to automatically replace parts of a string according to a correlation table. The correlation table shall be read from a file one time after starting the program, but then the data must be stored in RAM or similar so that the replacement is fast enough. Goal is to automatically replace any ".00", ".01", ... ,".99" (= maximal 100 values) of a given string by the values specified in the correlation table (and vice versa, when coding the string).

      Example:
      Content of the correlation table is: ABCD,GT,KF,H,CVP
      This shall stand for .00,.01,.02,.03,.04,...,.99 (= maximal 100 values)
      The correlation table shall be read after program start (or after a command) from e.g c:\table\table.dat, but data shall then come from RAM or similar.

      Decoder:
      Content of a given string which I read: gf46jkgj ghgo r .01
      Content of that string after automatical replacement: gf46jkgj ghgo r GT (= .01 replaced by GT)
      Content of a given string: gf46jkgj ghgo r .04
      Content of that string after automatical replacement: gf46jkgj ghgo r CVP (= .04 replaced by CVP)

      Coder:
      Content of a given value KF
      Content of the generated message before replacement: gf46jkgj ghgo r KF
      Content of the generated message after replacement: gf46jkgj ghgo r .02 (= KF replaced by .02)

      How I can I program something like that? (My C++ programming skills are not the best. Please be so kind and explain it well enough or at best give me a concrete example)

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 18 Feb 2022, 11:53 last edited by
      #2

      @Urbi There is https://doc.qt.io/qt-5/qstring.html#replace-5 which you can use to replace a string with another one.

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

      U 1 Reply Last reply 18 Feb 2022, 13:31
      2
      • J jsulm
        18 Feb 2022, 11:53

        @Urbi There is https://doc.qt.io/qt-5/qstring.html#replace-5 which you can use to replace a string with another one.

        U Offline
        U Offline
        Urbi
        wrote on 18 Feb 2022, 13:31 last edited by
        #3

        @jsulm Yes, but then I would need to program 100 individual replace commands. Isn't there a better way?

        M 1 Reply Last reply 18 Feb 2022, 15:16
        0
        • U Urbi
          18 Feb 2022, 13:31

          @jsulm Yes, but then I would need to program 100 individual replace commands. Isn't there a better way?

          M Offline
          M Offline
          mchinand
          wrote on 18 Feb 2022, 15:16 last edited by
          #4

          You can use a QMap or QHash to contain your replacement pairs. Extract that last part of the string that you want to replace, use that as the key in your map or hash to get the replacement string value, call the replace method on the entire string with that key/value pair.

          1 Reply Last reply
          3

          1/4

          18 Feb 2022, 10:40

          • 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