c# - Replace single chars in string at position x -
this question has answer here:
i doing simple hangman game. have gotten going except part user enters correct char , corresponding char in solution word should replaced former.
first, here code:
private void checkifletterisinword(char letter) { if (currentword != string.empty) { if (this.currentword.contains(letter)) { list<int> indices = new list<int>(); (int x = 0; x < currentword.length; x++) { if (currentword[x] == letter) { indices.add(x); } } this.enteredrightletter(letter, indices); } else { this.enteredwrongletter(); } } } private void enteredrightletter(char letter, list<int> indices) { foreach (int in indices) { string temp = lblword.text; temp[i] = letter; lblword.text = temp; } }
so problem line
temp[i] = letter;
i error here says "property or indexer cannot assigned — read only". have googled , found out strings cannot modified during runtime. have no idea how substitute label contains guesses. label in format
_ _ _ _ _ _ _ //single char + space
can give me hint how can replace chars in solution word guessed chars?
string immutable class, use stringbuilder instead:
... stringbuilder temp = new stringbuilder(lblword.text); temp[i] = letter; // <- possible here lblword.text = temp.tostring(); ...
Comments
Post a Comment