系统.数组不包含长度的定义
本文关键字:定义 包含长 数组 系统 | 更新日期: 2023-09-27 18:14:48
我尝试使用Windows控制台应用程序计算句子中的单词数。但我是数组的新手不知道该怎么处理这段代码
string sentence;
Console.WriteLine("Enter your sentence:");
sentence = Console.ReadLine();
string [] words = sentence.Split(' ');
Console.WriteLine(sentence.Lenght); // .Lenght is where I get the error from
Console.ReadKey();
在最简单的情况下(当word ==空格之间的任意字符时),您可以实现如下操作:
Console.WriteLine("Enter your sentence:");
// try not declaring local variable prematurely, but exacly where you want them
string sentence = Console.ReadLine();
// StringSplitOptions.RemoveEmptyEntries - what if user starts with space?
// separate words with two spaces, e.g. " This is a test input "
// we want 5, right?
int count = sentence
.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Length; // Please, notice spelling
Console.WriteLine(count);
Console.ReadKey();
我真的很笨,在我发帖之前没有确定我是否有拼写错误。现在,当你正确拼写长度词,它工作。对不起伙计们,但是,谢谢。