【发布时间】:2022-12-12 13:49:28
【问题描述】:
我怎样才能使 text.Split(' ')[0] 递增?我希望它执行 text.Split(' ')[++] 但是把那个 ++ 放在那里是行不通的。目标是让代码对“搜索”字词进行计数。抱歉,C# 新手。
using System;
namespace TESTER
{
class Program
{
static void Main(string[] args)
{
int wordCount = 0;
int index = 0;
string text = "I ate a donut on national donut day and it tasted like a donut";
string search = "donut";
// skip whitespace until first word
while (index < text.Length)
{
if (search == text.Split(' ')[0])
{
wordCount++;
}
}
Console.WriteLine(wordCount);
}
}
}
【问题讨论】:
-
text.Split(' ')[0]返回一个字符串。你不能使用++,除非它是一个数字 -
.Split返回一个数组,数组有一个.Length属性。你是在问如何遍历数组吗?
标签: c#