0

I am adding texts to a TextMeshPro but I do not know how can one delete the previously entered text from the textbox. All I have found online was string.empty() which I am not looking for. For example

public TMP_Text textMeshPro;

void Start()
{
   textMeshPro.text = "85" + "<sprite index=0>";
}

So in such a case given above, if I click on "backspace", the sprite should get deleted first, then if I press backspace again, 5 should get deleted and finally 8. That is each string should be deleted like how we do on a calculator. Is there a way to achieve this?

Mitch
  • 41
  • 5
  • @Lotan unfortunately no, it did not work. I tried to do ```textMeshPro.text.Remove (textMeshPro.text.Length - 1)```, but nothing happened. What is wrong with it? – Mitch Jun 04 '21 at 12:31
  • textMeshPro.text = textMeshPro.text.Remove(textMeshPro.text.Length - 1) and nothing happen? – Lotan Jun 04 '21 at 12:59
  • @Lotan tried that as well. nothing happened. – Mitch Jun 04 '21 at 13:11
  • this works as a way to remove the last letter of the string, I'm afraid your problem is related with something else that the removing stuff :( – Lotan Jun 04 '21 at 13:21
  • @Lotan it worked :) There was something wrong with my script as I was running multiple instances, I created new script and then ran, it worked. My one problem is I want to remove "" as one string/char. How do I achieve this? – Mitch Jun 04 '21 at 13:48
  • You would need to identify when you reach the `>` then keep deleting until the space after the `<`. – TEEBQNE Jun 04 '21 at 15:19

1 Answers1

0

I'm not sure this can be solve you're problem but here my advice:

    string text= textMeshPro.text.toString();
    string finalText=""
    textMeshPro.text=""
    for(int i=0; i<text.ToCharArray().Count()-1;i++){
    finalText+=text.ToCharArray()[i];
    }
    textMeshPro.text=finalText;

Basically, I suggest that: Get the whole text as a string and convert them to char array after that you can control each one of char.

HTugsadK
  • 81
  • 5