c#编码从一个句子中提取单词,其中句子由用户自己输入

来源:百度知道 编辑:UC知道 时间:2024/05/30 11:35:55
从一个句子中提取单词,其中句子由用户自己输入(提示:使用字符串的split函数)。
如,用户输入的英文句子是:“Tom,how do you do?”;回车之后控制台输出为:
Tom
how
do
you
do

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class NamedWelcome
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个句子");
string str = Console.ReadLine();
string[] split = str.Split(new char[] { ' ', ',', '.', '!' });
foreach (string s in split)
{
if (s.Trim() != "")
Console.WriteLine("\n"+s);
}
Console.ReadLine();

}
}
}

string str="Tom,how do you do?";
//把所有标点转换成空格
str=str.replace(","," ").replace("."," ").replace("?"," ").replace(";"," ").replace(":","");