获取一句话里面英文单词个数怎么实现? 要用c#实现

如题

第1个回答  2010-06-04
string str="......";
int z=0;
for(int i=0;i<str.lengh;i++)
{
if(str[i]>'a' && str[i]<'z' || str[i]>'A' && str[i]<'Z')
z++;
}
z英文字母就是个数
第2个回答  2010-06-04
string[] s="hello everybody".split(convert.tochar(" "));
s.length即为个数
不过如果有逗号,引号之类的。可能就需要正则了。
第3个回答  2010-06-04
“〔a-zA-z']*” 建议用正则
第4个回答  2010-06-04
protected void Button1_Click(object sender, EventArgs e)
{
//正则表达式
string pattern = @"(?<Character>[a-zA-Z])";
Regex r = new Regex(pattern);
MatchCollection mc = r.Matches("Hello12你好World!");
int strNum = 0;
foreach (Match m in mc)
{
strNum++;
}
Response.Write(strNum.ToString());
}本回答被提问者采纳
第5个回答  2010-06-04
string str="......";
int z=0;
for(int i=0;i<str.lengh;i++)
{
if(str[i]>'a' && str[i]<'z' || str[i]>'A' && str[i]<'Z')
{
while(str[i+1]>'a' && str[i+1]<'z' || str[i+1]>'A' && str[i+1]<'Z') //翻越一个单词的字母;
{
i++;
}
z++; //记录单词个数;
}
}
相似回答