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 to backspace ?
Qt 6.11 is out! See what's new in the release blog

How to replace to backspace ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 753 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote last edited by sonichy
    #1
    123
    456
    789
    

    Replace to

    123
    789
    

    Target

    s.replace("456", "\b");
    

    Actual

    s.replace("456", ""); // delete leave empty line
    s.replace(QRegExp("\\n{2,}"), "\n"); // delete empty line
    

    https://github.com/sonichy

    JonBJ 1 Reply Last reply
    0
    • sonichyS sonichy
      123
      456
      789
      

      Replace to

      123
      789
      

      Target

      s.replace("456", "\b");
      

      Actual

      s.replace("456", ""); // delete leave empty line
      s.replace(QRegExp("\\n{2,}"), "\n"); // delete empty line
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote last edited by JonB
      #2

      @sonichy
      As usual your posts are so cryptic and short on a question that it's difficult to know what situation you are in.

      It is unusual for a string to have a Backspace in it, as normally that deletes the previous character and you would never receive it in string passed back, so be sure you know what you are doing.

      I am guessing your s is a QString, but this is the sort of thing you are supposed to say when posing a question. I do not know what you think you are doing with s.replace("*", "\b"); and s.replace("*", "");. Those both replace a literal * with a literal backspace or nothing respectively. Is that what you intend? If by any chance the * is supposed to have something to do with trying to be a regular expression you are not using the correct overload. As for s.replace("\n\n\n", "\n"); // delete empty line, it replaces two adjacent empty lines not one.

      So if you want to actually ask a proper question please rephrase.

      1 Reply Last reply
      0
      • sonichyS Offline
        sonichyS Offline
        sonichy
        wrote last edited by sonichy
        #3

        OK,I change it to sample.
        s = all text.
        * = selected text.

        https://github.com/sonichy

        JonBJ 1 Reply Last reply
        0
        • sonichyS sonichy

          OK,I change it to sample.
          s = all text.
          * = selected text.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote last edited by
          #4

          @sonichy
          You have changed the content of your original post. I don't know what your second post means. I don't know what your "\b" is all about. I don't know what your exact question is or if there is a question.

          From what you now show in your original post you can remove the line reading 456, which is what you say you want to do, via s.replace("456\n", "");. For anything else perhaps someone else will understand what you are asking, I do not so I will leave it to others.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote last edited by
            #5

            Hi,

            s.remove(QRegularExpression("456\n"));
            

            QRegExp has been deprecated and removed. Don't use it.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SimonSchroeder
              wrote last edited by
              #6

              It is not so easy to decipher what the actual problem is. However, here is what I observe. We start with the following string:

              "123\n456\n789"
              

              If you just want to delete something from the string, you would replace it with an empty string. So,

              s.replace("456", "");
              

              would yield

              "123\n\n789"
              

              This means, that this would print an empty line between 123 and 789.

              However, if you do

              s.replace("456", "\b");
              

              you'll get

              "123\n\b\n789"
              

              I guess that like most people here I haven't ever used \b myself. Not all implementations that print out strings to the console (or wherever) might have an appropriate implementation of \b. Hence, this might vary. One possible implementation is that \b will indead delete the previous character when printed. In this case \n would be removed. This would result in the same output as

              "123\n789"
              

              However, another implementation might print line by line. So, when \b is encountered a new line has already begun. And on the new line there is nothing to delete. The command line is not a text editor where there is a document behind it and everything is rerendered when you change something. On the same line the implementation is quite simple: go back one character and replace it with blank (but keep the cursor at the same position). However, if the command line has to go back one line, it does not have the information stored where the previous line ended. It is quite impossible (without buffering) to go back to the previous line. After a new line you (most likely) have committed to what is printed on the screen (in most implementations).

              Note that you'll insert \b into the string. This does not delete the previous character inside the string. The string itself is just bytes and contains what you put in it.

              If you want to delete the full line containing 456, you should write

              s.replace("456\n", "");
              
              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