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 二胎祝福语简短霸气
18 小清新的祝福语简短