计算字符串变量中的字母数

And*_*rew 6 c# string

我正在尝试计算字符串变量中的字母数.我想制作一个Hangman游戏,我需要知道需要多少个字母来匹配单词中的数量.

It'*_*ie. 45

myString.Length; //will get you your result
//alternatively, if you only want the count of letters:
myString.Count(char.IsLetter);
//however, if you want to display the words as ***_***** (where _ is a space)
//you can also use this:
//small note: that will fail with a repeated word, so check your repeats!
myString.Split(' ').ToDictionary(n => n, n => n.Length);
//or if you just want the strings and get the counts later:
myString.Split(' ');
//will not fail with repeats
//and neither will this, which will also get you the counts:
myString.Split(' ').Select(n => new KeyValuePair<string, int>(n, n.Length));
Run Code Online (Sandbox Code Playgroud)

  • @ CL4PTR4P:在刽子手游戏中你不想算空间.你想分别处理每个单词,在单词之间留一个空格.所以:"我的刽子手问题"=>"__ _______ ________" (2认同)