为了账号安全,请及时绑定邮箱和手机立即绑定

使用字符串 [] 或字符串列表计算单词?

使用字符串 [] 或字符串列表计算单词?

C#
胡说叔叔 2021-06-29 13:48:15
我试图计算文件中的单词数,要么我做一个 string[] 列表并在取出空格时出错,要么我做普通字符串并在 Splitting String 部分出错,我也想显示三个大多数重复的单词这就是为什么我需要一个所有字符串的列表。代码如下://Reading Filevar path = @"D:\Projects\C sharp Course\Working_with_Files\Working_with_Files_1\Text.txt";List<string> list = new List<string>();var content = File.ReadAllText(path);var text = content.Trim();string[] splitted;//Splitting Stringfor (int i = 0; i < text.Length; i++){    splitted = text.Split(',', ' ', '.', '(', ')');              list.Add(splitted);}//Taking out Whitespacesfor (int i = 0; i < list.Count; i++){    if (string.IsNullOrWhiteSpace(list[i]))    {        list.RemoveAt(i);    }}
查看完整描述

2 回答

?
动漫人物

TA贡献1815条经验 获得超10个赞

你的循环似乎没有意义,因为每个循环都会做同样的事情:


for (int i = 0; i < text.Length; i++)//You do not use this i!

{

    splitted = text.Split(',', ' ', '.', '(', ')');          

    list.Add(splitted);//Will add the same splitted text each time.

}

我认为您可以删除循环,因为拆分已经拆分了整个文本。


string[] splitted = text.Split(',', ' ', '.', '(', ')');       


查看完整回答
反对 回复 2021-07-10
  • 2 回答
  • 0 关注
  • 128 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信