通过阅读字符跳过空白区域

Inf*_*und -2 c#

我想跳过字符之间的空格.例如,我有这个:

abc def ghi,那么输出必须是:

a = 1
b = 1
c = 1..etc
Run Code Online (Sandbox Code Playgroud)

但现在我得到:

"" = 2.
Run Code Online (Sandbox Code Playgroud)

因为角色之间有两个空格.

我试试这样:

SortedDictionary<char, int> dictionary = new SortedDictionary<char, int>();

Console.WriteLine("Enter a string: "); // prompt for user input
string input = Console.ReadLine(); // get input

// split input text into tokens
char[] letters = Regex.Split(input.ToCharArray().ToString(), @"\s+");
Run Code Online (Sandbox Code Playgroud)

kri*_*gar 5

只需省略数组中的空格:

using System.Linq;

// Your other code
char[] letters = input.Where(c => c != ' ').ToArray();
Run Code Online (Sandbox Code Playgroud)

字符串本身就是一个char数组,因此您无需使用该调用进行强制转换.