std::string and Backspaces

Started by
4 comments, last by Brother Bob 13 years, 8 months ago
Is there anyway I can store backspaces in a string and then add it to another string to remove characters? This is what I tried to do but the backspaces had no effect.

std::string a = "\b\b\b\b";std::string b = "abcd";b += a;
Advertisement
No, it'll just append the characters in a onto b.
You could write a function that inspects b afterwards to do the removal.
Quote:Original post by Hodgman
No, it'll just append the characters in a onto b.
You could write a function that inspects b afterwards to do the removal.


Alright thanks. I have a function that does it but I'm surprised there isn't an easier/nicer way.
TBH, I've never seen a program that used the '\b' special character!

Also, std::basic_string is just a container class - all it's supposed to do is contain a sequence of 'characters' (which may or may not be char).
std::string is just another name for std::basic_string<char>, so you could just as easily have a std::basic_string<float>, which obviously won't recognise '\b'.

By assigning what is basically a function-callback to special-characters within strings, you're elevating std::string from being a container-class to being a code-interpreter.
Quote:Original post by Hodgman
TBH, I've never seen a program that used the '\b' special character!

Also, std::basic_string is just a container class - all it's supposed to do is contain a sequence of 'characters' (which may or may not be char).
std::string is just another name for std::basic_string<char>, so you could just as easily have a std::basic_string<float>, which obviously won't recognise '\b'.

By assigning what is basically a function-callback to special-characters within strings, you're elevating std::string from being a container-class to being a code-interpreter.


You're right, that does make sense. Why wouldn't you use the '\b' character to implement some sort of backspace functionality though in a textbox or something? This is what I did:

void updateString(std::string& currentString, const std::string& stringToAdd) {    for (size_t i = 0; i < stringToAdd.size(); ++i) {        if (stringToAdd == '\b') {            if (!currentString.empty()) {                currentString.erase(currentString.end() - 1);            }        } else {            currentString += stringToAdd;        }    }}


Quote:Original post by Jethro_T
Why wouldn't you use the '\b' character to implement some sort of backspace functionality though in a textbox or something?

You would implement the backspace functionality, along with other editing features, in the textbox code, not in the string it contains. Whenever you press backspace, remove the character from the string, instead of inserting a backspace character and later normalize the string.

Since you mentioned texbox, how would you, say, implement arbitrary carret placement and editing? Say the user want to remove a character in the middle of the string? Is he forced to backspace half the string, or how do you encode when the user pressed the mouse at the undesired character to place the carret there, and then remove it with a single backspace? And can you then insert characters into the textbox other than at the end? How about selecting a range of characters to replace or remove?

As your textbox grows with useful features, your string doesn't contain the textbox string anymore, it contains the characters and mouse movements needed to replicate the string. As Hodgman said, what you end up with is not a container class, but a code interpreter; it contains the code (keyboard and mouse movements) you need to replay to replicate the string in the textbox that the user wanted.

Let the textbox handle the textbox functions, and the string contain the string.

This topic is closed to new replies.

Advertisement