c#窗体应用输入字符串后单击按钮统计多少个单词

如图所示,输入一个字符串后单击按钮,统计其中有多少个单词,单词之间用空格分隔如何实现

img

把方法改成窗体代码就可以了。如有帮助,请采纳


//代码如下,调试结果稍后到。


using System;

class CountWords   {

static void Main(string[] args)

{

/* 初始化计数器,读入字符串*/      


int ct=0, i, j=0;


Console.WriteLine("input a list of words:");


string rd = Console.ReadLine();




 for(i=0;i<rd.Length;i+=1){


  if(rd[i]<='z'&&rd[i]>='a')j=1; 


   else if(rd[i]<='Z'&&rd[i]>='A')j=1;


  else if(j==1){j=0;ct+=1;}


 }

 if(j==1){j=0;ct+=1;}


  




 Console.WriteLine("\nThe number of word(s) is: {0}",ct);

Console.ReadKey();


}

}

看你具体的需求,示例如下:

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var list = new[]
            {
                "Hello world",
                "    Hello world",
                "Hello world    ",
                "Hello      world",
                "Hello      world!",
                "Hello world ... ",
                "  Hello   world ... "
            };
            foreach (var item in list)
            {
                Console.WriteLine("----------------------------------");
                Console.WriteLine($"方法CountWords1统计的单词数: {CountWords1(item)}");
                Console.WriteLine($"方法CountWords2统计的单词数: {CountWords2(item)}");
                Console.WriteLine($"方法CountWords3统计的单词数: {CountWords3(item)}");
                Console.WriteLine($"方法CountWords4统计的单词数: {CountWords4(item)}");
                Console.WriteLine("----------------------------------");
            }
            Console.ReadKey();
        }

        static int CountWords1(string input)
        {
            var collection = Regex.Matches(input, @"[\S]+");
            return collection.Count;
        }
        static int CountWords2(string input)
        {
            var c = 0;
            for (var i = 1; i < input.Length; i++)
            {
                if (char.IsWhiteSpace(input[i - 1]) != true) continue;
                if (char.IsLetterOrDigit(input[i]) || char.IsPunctuation(input[i]))
                {
                    c++;
                }
            }
            if (input.Length > 2)
            {
                c++;
            }
            return c;
        }

        static int CountWords3(string input) => input.Split(' ').Length;

        static int CountWords4(string input)
        {
            return Regex.Replace(input, "[^a-zA-Z0-9_]+", " ")
                .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                .Length;
        }
    }
}

运行结果:

----------------------------------
方法CountWords1统计的单词数: 2
方法CountWords2统计的单词数: 2
方法CountWords3统计的单词数: 2
方法CountWords4统计的单词数: 2
----------------------------------
----------------------------------
方法CountWords1统计的单词数: 2
方法CountWords2统计的单词数: 3
方法CountWords3统计的单词数: 6
方法CountWords4统计的单词数: 2
----------------------------------
----------------------------------
方法CountWords1统计的单词数: 2
方法CountWords2统计的单词数: 2
方法CountWords3统计的单词数: 6
方法CountWords4统计的单词数: 2
----------------------------------
----------------------------------
方法CountWords1统计的单词数: 2
方法CountWords2统计的单词数: 2
方法CountWords3统计的单词数: 7
方法CountWords4统计的单词数: 2
----------------------------------
----------------------------------
方法CountWords1统计的单词数: 2
方法CountWords2统计的单词数: 2
方法CountWords3统计的单词数: 7
方法CountWords4统计的单词数: 2
----------------------------------
----------------------------------
方法CountWords1统计的单词数: 3
方法CountWords2统计的单词数: 3
方法CountWords3统计的单词数: 4
方法CountWords4统计的单词数: 2
----------------------------------
----------------------------------
方法CountWords1统计的单词数: 3
方法CountWords2统计的单词数: 4
方法CountWords3统计的单词数: 8
方法CountWords4统计的单词数: 2
----------------------------------