C#:将文本分解为单词

static void Main(string[] args)
{
    var text = "Youth is not a time of life";
    string[] words = Regex.Split(text,@"\W+");//这句代码就是将文本转为单词,但是需要添加这一句引用  using System.Text.RegularExpressions;
    var i = 0;
    foreach(var word in words)
    {
        i++;
        Console.WriteLine("第{0}个单词是:{1}",i,word);
    }
    Console.WriteLine("这个句子由{0}个单词组成",words.Length);
    Console.ReadKey();
}

输出结果:

猜你喜欢

转载自blog.csdn.net/yytIcon/article/details/82797216
今日推荐