System.Array不包含Length的定义

时间:2016-10-24 10:21:11

标签: c# console-application

我尝试使用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();

2 个答案:

答案 0 :(得分:1)

最简单的情况下(当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();

答案 1 :(得分:0)

我真的很蠢,以至于在我发布这里之前不确定是否有任何拼写错误。现在你正确拼写长度单词,它的确适用。对不起,但是,谢谢。