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 427 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 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)

    jsulmJ 1 Reply Last reply
    0
    • U Urbi

      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)

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

        @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 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
        0
        • U Urbi

          @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 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

          • Login

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