How to replace to backspace ?
-
123 456 789Replace to
123 789Target
s.replace("456", "\b");Actual
s.replace("456", ""); // delete leave empty line s.replace(QRegExp("\\n{2,}"), "\n"); // delete empty line@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
sis aQString, 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 withs.replace("*", "\b");ands.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 fors.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.
-
@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, vias.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. -
Hi,
s.remove(QRegularExpression("456\n"));QRegExp has been deprecated and removed. Don't use it.
-
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
\bmyself. 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\bwill indead delete the previous character when printed. In this case\nwould be removed. This would result in the same output as"123\n789"
However, another implementation might print line by line. So, when
\bis 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
\binto 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", "");