大家好,所以我需要在顯示的文本框中的每個字符之間添加一個「空格」。在C#文本框中添加'空格'
我給用戶一個蒙面字像這樣
He__o
讓他猜,我想這與轉換爲H e _ _ o
我使用下面的代碼來隨機替換字符
'_'
char[] partialWord = word.ToCharArray(); int numberOfCharsToHide = word.Length/2; //divide word length by 2 to get chars to hide Random randomNumberGenerator = new Random(); //generate rand number HashSet<int> maskedIndices = new HashSet<int>(); //This is to make sure that I select unique indices to hide. Hashset helps in achieving this for (int i = 0; i < numberOfCharsToHide; i++) //counter until it reaches words to hide { int rIndex = randomNumberGenerator.Next(0, word.Length); //init rindex while (!maskedIndices.Add(rIndex)) { rIndex = randomNumberGenerator.Next(0, word.Length); //This is to make sure that I select unique indices to hide. Hashset helps in achieving this } partialWord[rIndex] = '_'; //replace with _ } return new string(partialWord);
我曾嘗試:
partialWord[rIndex] = '_ ';
然而這帶來的錯誤「在文本字符太多」我曾嘗試過:
partialWord[rIndex] = "_ ";
但是,這會返回錯誤「無法將類型字符串轉換爲字符。
任何想法如何才能繼續實現每個字符之間的空間?
感謝
不重複(據我所知),但有一點谷歌搜索,你會發現:http://stackoverflow.com/questions/7189293/add-spaces-between-the-characters-of -a-string-in-java,你可以直接複製這些循環。 – VinKel