C#程序计算字符串中的单词数
让我们首先声明字符串-
string str = "你好,世界!";
现在遍历完整的字符串,找到空白或制表符或换行符-
while (a <= str.Length - 1) {
if(str[a]==' ' || str[a]=='\n' || str[a]=='\t') {
myWord++;
}
a++;
}示例
让我们看完整的代码,以计算C#中字符串中的单词数。
using System;
public class Demo {
public static void Main() {
int a = 0 , myWord = 1;
string str = "你好,世界!";
while (a <= str.Length - 1) {
if(str[a]==' ' || str[a]=='\n' || str[a]=='\t') {
myWord++;
}
a++;
}
Console.Write("Number of words in the string = {0}\n", myWord);
}
}输出结果
Number of words in the string = 2
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短