removing characters from a string

Traitée removing characters from a string

  • mardi 1 mai 2012 16:07
     
      A du code

    Hi,

    I have the following problem..

    I have a string which is hidden i.e. "-----", the string is "hello"

    I figure out the second letter is an "e" by using lastIndexof, I then insert "e" in to the position but I am unable to remove the - that was there initially.

      int a = words.Word.LastIndexOf(WordGuesses);
    
      string b = words.HideWord.Insert(a, WordGuesses);
                    
       words.HideWord = b;


    I have tried remove, using 

    words.HideWord = words.HideWord.Remove(words.HideWord.Length - 1, 1);

    but this just removes the last entry also tried trim.

    What I want to do is find the position of the new letter remove the "-" and insert the letter in its place any ideas..?

    Thanks

Toutes les réponses

  • mardi 1 mai 2012 16:14
     
     
    Hmm, not sure what you want to do. String is immutable (the contents of a string object cannot be changed after the object is created), so you cannot change it. Why you jut dont assign a new value to variable?

    Mitja

  • mardi 1 mai 2012 16:18
     
     

    Thanks for the reply, but I can change the string as its a property but when I do it doesn't remove the '-'.

    I want to be able to insert - - - - -   "E" in to the second dash, I can do this but I want it to remove the second dash before it adds the "E"

    Any more ideas..?

  • mardi 1 mai 2012 16:26
     
     Traitée A du code

    For this you really don't want to be using a string as insert/remove methods are not cheap.

    Store both the real word and a char array.  The char array will start out with all dashes.  Then to change the 2nd position to an 'e' you can simply write out:

    myCharArray[2] = 'e';

    You can then, at any time, get a string representation of that char array by using:

    string someString = new String(myCharArray);

    You can use that to set a label, or whatever other means of displaying it you're using.