Jump to content
  • Advertisement
Sign in to follow this  
Jethro_T

std::string and Backspaces

This topic is 2949 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}
}
}



Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!