C# 如何正确计算文本中的单词数

C# 如何正确计算文本中的单词数,c#,visual-studio-2010,C#,Visual Studio 2010,我想给我的程序一个文本,它正确地计算单词 我尝试使用数组在其中保存单词: string[] words = richTextBox1.Text.Split(' '); 但是这段代码有一个问题,就是要计算文本中的空格 因此,我尝试了以下代码: string[] checkwords= richTextBox1.Text.Split(' '); for (int i = 0; i < checkwords.Length; i++) {

我想给我的程序一个文本,它正确地计算单词 我尝试使用数组在其中保存单词:

string[] words = richTextBox1.Text.Split(' ');
但是这段代码有一个问题,就是要计算文本中的空格 因此,我尝试了以下代码:

 string[] checkwords= richTextBox1.Text.Split(' ');
        for (int i = 0; i < checkwords.Length; i++)
        {
            if (richTextBox1.Text.EndsWith(" ") )
            {
                return;
            }

            else
            {
                string[] words = richTextBox1.Text.Split(' ');

                toolStripStatusLabel1.Text = "Words" + " = " + words.Length.ToString();
string[]checkwords=richTextBox1.Text.Split(“”);
for(int i=0;i

但是现在它无法正常工作。

我建议在这里使用
Regex
,使用锚定

否则,您的代码可能无法正确地考虑选项卡和新行之类的内容-
\b
将为您解决这些问题

var words = Regex
    .Split("hello world", @"\b")
    .Where(s => !string.IsNullOrWhiteSpace(s));

var wordCount = words.Count();

我建议在这里使用
Regex
,使用锚点

否则,您的代码可能无法正确地考虑选项卡和新行之类的内容-
\b
将为您解决这些问题

var words = Regex
    .Split("hello world", @"\b")
    .Where(s => !string.IsNullOrWhiteSpace(s));

var wordCount = words.Count();

我建议在这里使用
Regex
,使用锚点

否则,您的代码可能无法正确地考虑选项卡和新行之类的内容-
\b
将为您解决这些问题

var words = Regex
    .Split("hello world", @"\b")
    .Where(s => !string.IsNullOrWhiteSpace(s));

var wordCount = words.Count();

我建议在这里使用
Regex
,使用锚点

否则,您的代码可能无法正确地考虑选项卡和新行之类的内容-
\b
将为您解决这些问题

var words = Regex
    .Split("hello world", @"\b")
    .Where(s => !string.IsNullOrWhiteSpace(s));

var wordCount = words.Count();

如果文本框以“”结尾,我不知道您为什么希望返回。也许它应该是下一个或继续

如果可能有多个空格

Regex myRege = new Regex(@"[ ]{2,}");     

string myText = regex.Replace(richTextBox1.Text, @" ");

string[] words= myText.Split(" ");

toolStripStatusLabel1.Text = "Words" + " = " + words.Length.ToString();
只是为了好玩

private string[] GetCount(string bodyText)
{
  bodyText = bodyText.Replace("  "," ");

  if(bodyText.Contains("  ")
    GetCount(bodyText)

  return bodyText.Split(' ');
}

string[] words = GetCount(richTextBox1.Text)

toolStripStatusLabel1.Text = "Words" + " = " + words.Length.ToString();

如果文本框以“”结尾,我不知道您为什么希望返回。也许它应该是下一个或继续

如果可能有多个空格

Regex myRege = new Regex(@"[ ]{2,}");     

string myText = regex.Replace(richTextBox1.Text, @" ");

string[] words= myText.Split(" ");

toolStripStatusLabel1.Text = "Words" + " = " + words.Length.ToString();
只是为了好玩

private string[] GetCount(string bodyText)
{
  bodyText = bodyText.Replace("  "," ");

  if(bodyText.Contains("  ")
    GetCount(bodyText)

  return bodyText.Split(' ');
}

string[] words = GetCount(richTextBox1.Text)

toolStripStatusLabel1.Text = "Words" + " = " + words.Length.ToString();

如果文本框以“”结尾,我不知道您为什么希望返回。也许它应该是下一个或继续

如果可能有多个空格

Regex myRege = new Regex(@"[ ]{2,}");     

string myText = regex.Replace(richTextBox1.Text, @" ");

string[] words= myText.Split(" ");

toolStripStatusLabel1.Text = "Words" + " = " + words.Length.ToString();
只是为了好玩

private string[] GetCount(string bodyText)
{
  bodyText = bodyText.Replace("  "," ");

  if(bodyText.Contains("  ")
    GetCount(bodyText)

  return bodyText.Split(' ');
}

string[] words = GetCount(richTextBox1.Text)

toolStripStatusLabel1.Text = "Words" + " = " + words.Length.ToString();

如果文本框以“”结尾,我不知道您为什么希望返回。也许它应该是下一个或继续

如果可能有多个空格

Regex myRege = new Regex(@"[ ]{2,}");     

string myText = regex.Replace(richTextBox1.Text, @" ");

string[] words= myText.Split(" ");

toolStripStatusLabel1.Text = "Words" + " = " + words.Length.ToString();
只是为了好玩

private string[] GetCount(string bodyText)
{
  bodyText = bodyText.Replace("  "," ");

  if(bodyText.Contains("  ")
    GetCount(bodyText)

  return bodyText.Split(' ');
}

string[] words = GetCount(richTextBox1.Text)

toolStripStatusLabel1.Text = "Words" + " = " + words.Length.ToString();

您可以将的重载与
StringSplitOptions.RemoveEmptyEntries一起使用,以忽略多个连续空格

string text = "a    b c    d";  // 4 "words"
int words = text.Split(new char[]{}, StringSplitOptions.RemoveEmptyEntries).Length;  

我使用的是一个空的
char[]
(您也可以使用
新字符串[]{}
),因为这考虑了所有因素,所以不仅
'
,还可以使用制表符或新行字符。

您可以使用with
StringSplitOptions的重载。RemomptyEntries
忽略多个连续空格

string text = "a    b c    d";  // 4 "words"
int words = text.Split(new char[]{}, StringSplitOptions.RemoveEmptyEntries).Length;  

我使用的是一个空的
char[]
(您也可以使用
新字符串[]{}
),因为这考虑了所有因素,所以不仅
'
,还可以使用制表符或新行字符。

您可以使用with
StringSplitOptions的重载。RemomptyEntries
忽略多个连续空格

string text = "a    b c    d";  // 4 "words"
int words = text.Split(new char[]{}, StringSplitOptions.RemoveEmptyEntries).Length;  

我使用的是一个空的
char[]
(您也可以使用
新字符串[]{}
),因为这考虑了所有因素,所以不仅
'
,还可以使用制表符或新行字符。

您可以使用with
StringSplitOptions的重载。RemomptyEntries
忽略多个连续空格

string text = "a    b c    d";  // 4 "words"
int words = text.Split(new char[]{}, StringSplitOptions.RemoveEmptyEntries).Length;  

我使用的是空的
char[]
(您也可以使用
新字符串[]{}
),因为这考虑了所有因素,所以不仅
'
,而且还有制表符或新行字符。

正确定义你有没有考虑过中文?希伯来语?UTF16?显示一个你试图计数的单词的例子……你甚至已经通过代码了吗?……在你的问题中清理/格式化这个代码……也重新读取你的代码在<代码>代码中>你已经分裂了RetryText Box1.Text。为什么你在String []中再次这样做?单词..?非常不清楚你在问什么我投票关闭为什么拆分文本,运行for循环,如果原始字符串以空格结尾则中止循环?如果结尾没有空格,则再次拆分文本有什么意义?检查是否以空格结尾,然后拆分/计数是否要多次计算空格?然后使用上盖string.split的加载:
int countWords=richTextBox1.Text.split(新[]{'},StringSplitOptions.RemoveEmptyEntries)。长度;
@sehe我知道,我只是想笑一下:)定义“正确”你有没有考虑过中文?希伯来语?UTF16?显示一个你试图计数的单词的例子……你甚至已经通过代码了吗?……在你的问题中清理/格式化这个代码……也重新读取你的代码在<代码>代码中>你已经分裂了RetryText Box1.Text。为什么你在String []中再次这样做?单词..?非常不清楚你在问什么我投票关闭为什么拆分文本,运行for循环,如果原始字符串以空格结尾则中止循环?如果结尾没有空格,则再次拆分文本有什么意义?检查是否以空格结尾,然后拆分/计数是否要多次计算空格?然后使用上盖string.split的加载:
int countWords=richTextBox1.Text.split(新[]{'},StringSplitOptions.RemoveEmptyEntries)。长度;
@sehe我知道,我只是想笑一下:)定义“正确”你有没有考虑过中文?希伯来语?UTF16?显示一个你试图计数的单词的例子……你甚至已经通过代码了吗?……在你的问题中清理/格式化这个代码……也重新读取你的代码在<代码>代码中>你已经分裂了RetryText Box1.Text。为什么你在String []中再次这样做?单词..?非常不清楚你在问什么我投票关闭为什么拆分文本,运行for循环,然后在原始字符串以空格结尾时中止循环?如果结尾没有空格,再次拆分文本有什么意义?请检查sp是否以空格结尾