Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Subtracting strings

Discussion in 'Scripting' started by dezert99, Jan 8, 2015.

  1. dezert99

    dezert99

    Joined:
    Nov 15, 2014
    Posts:
    11
    Hi! Im running into a little problem I am creating a game and one of the components is that you have to enter text that is going to be showed in a Gui.Label. Here is how i do that:
    Code (CSharp):
    1. void OnGUI() {
    2.         GUI.Label(new Rect(10, 10, 100, 20), stringToEdit);
    3.     }
    4. void Update(){
    5.        
    6.         if(GM.Lockin&& !(Input.GetKeyDown(KeyCode.Backspace))){
    7.             stringToEdit = stringToEdit + Input.inputString;
    8.            
    9.         }
    10. }
    The lockin variable is set when the user left clicks so that if they press wasd they wont be walking. Now if they make a mistake then I want them to be able to remove some of the string so here is what i have for that.
    Code (CSharp):
    1. if(Input.GetKeyDown (KeyCode.Backspace)){
    2.  
    3.             if(stringToEdit.Length==1){
    4.                 stringToEdit=string.Empty;
    5.             }
    6.             else stringToEdit=stringToEdit.Remove(stringToEdit.Length-2);
    7. }
    The reason i have to subtract by two is because the backspace is being entered into the string as well so I have to remove that as well. I have the if stringToEdit==1 because i will get an error that im subtracting too much. I am displaying stringToEdit.Length in the console and I see that it does change to 0 after the string.Empty is called but something keeps adding something to the string after that happens. I have to compare this to something later on so it messes up the comparison. even though the text will be exactly the same the stringToEdit will have an extra length then it should. Any ideas?Ive been stuck on this for quite some time. Anything can help really. Thanks!
    ~Dezert99~
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I don't know if this is why, but you can get in trouble using 2 if's for that type of test. It's better to use an if, else. Then it will always only do one of the two statements. 2 ifs may do both if something changed and the other if then works. I would think it would be the backspace in there, but who knows. Otherwise maybe clear it by making it equal to "" instead, maybe string.empty does something different. Beats me really, just trying to help.
     
  3. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
  4. dezert99

    dezert99

    Joined:
    Nov 15, 2014
    Posts:
    11
    Thank you so much for your responses and so fast too! Im sorry i never responded.. I had it set up to email me if anyone posted but it never did. Im going to try these out now.
     
  5. dezert99

    dezert99

    Joined:
    Nov 15, 2014
    Posts:
    11
    I was still not able to get this to work. Can you give me an example of what to do ?