Help with strings

Hello guys I’m running into an issue. I have a string that I want to lose a letter if backspace is pressed. Here is my code.
if(Input.GetKeyDown (KeyCode.Backspace)){
stringToEdit= stringToEdit.Substring(0,stringToEdit.Length-1);
Debug.Log (“Backspace is pressed”);
}
This should work and I know it has something to do with the stringToEdit.Lenght. If i put in my own value for the length it would work just fine. Does anyone know why this is happening?
Incase you need to see the entire code here it is.
using UnityEngine;
using System.Collections;

public class Gui : MonoBehaviour {
public string stringToEdit = “”;
public GameManager GM=null;
void OnGUI() {
GUI.Label(new Rect(10, 10, 100, 20), stringToEdit);
}
void Update(){
if(GM.Lockin){
stringToEdit = stringToEdit + Input.inputString;
Debug.Log (stringToEdit);
}
if(Input.GetKeyDown (KeyCode.Backspace)){
stringToEdit= stringToEdit.Substring(0,stringToEdit.Length-1);
Debug.Log (“Backspace is pressed”);
}

}
}

Oh and also im running into a problem where when im checking the value of what the user entered in stringToEdit it wont see the answer as correct if the user enters lets say bob. But if i hard code the value for stringToEdit as bob it will work jut fine. Any ideas on why that’s happening as well?
void OnTriggerStay(Collider other){
Debug.Log (element+“User is in trigger”);
if (mygui.stringToEdit == answer) {
answer.text = “Correct!”;
Debug.Log (“Correct”);
}

}

I can’t test it at the moment, but a first guess:

From my understanding you add the backspace to the string, too so I would try the following:

stringToEdit = stringToEdit.Substring(0, stringToEdit.Length - 2);

If you check the input for “bob”, what do you see?

Thank you so much! That solved the first problem. What do you mean for the second?

Anyone else know why the second part might be happening

You said

The question is: If the user enters “bob” what is the value you see (e.g. with Debug.Log)?

Oh I see. And it will say bob In the console. That’s what confuses me because it’s the same exact thing as the answer but it won’t display correct if the user enters it

Again, what is the incorrect value you see on the screen (I guess you referring to GUI.Label)?

Yes I am referring to a label

o.k. and what do you see?

I see bob on the label

mmhh, now I’m a bit lost.
Can you write the text and the length of answer to the console, and the same with mygui.stringToEdit and maybe for GUI.Label, too.
Can you see any differences?

Oh my gosh I just got it. The enter key was also being counted as input for the text so it had an extra space in the length. I tried everything to try and substring i kept getting length issues but then i decided to just make a mouse click be used to start the text input. Thank you for all your help!