Trying to get Javascript to delete the end characters on a stored string further than the last character

StackOverflow https://stackoverflow.com/questions/22162276

  •  19-10-2022
  •  | 
  •  

Pergunta

So I'm reading characters into a string in Javascript. I want the user to be able to delete characters they have stored, much as one would do in word processing.

function Update() {
    if (Input.GetKeyDown("return")) c+= "\n";
    if (Input.GetKeyDown("tab")) c+= "     ";
    if (Input.GetKeyDown("backspace")) c = c.Substring(0, c.Length - 1);
    if (Input.inputString.Length != 0)
    {
        c += Input.inputString;
        guiText.text = c;
    }
}

The issue I'm running into is that after hitting backspace once, it stops going further back- I can only delete one character. For example, were I to type "example", and then hit backspace twice, I would have "exampl", whereas I would want to have "examp".

I'd love some help on figuring out where I'm going wrong here :) Thanks!

Nenhuma solução correta

Outras dicas

Full code in your question would have been better, You are storing one character at a time right? Try

function Update() {
    if (Input.GetKeyDown("return")) c+= "\n";
    else if (Input.GetKeyDown("tab")) c+= "     ";
    else if (Input.GetKeyDown("backspace")) c = c.Substring(0, c.Length - 1);
    else if (Input.inputString.Length != 0)  c += Input.inputString;    

    guiText.text = c;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top