Im trying to counting the Words in a File, either i do a list of string[] and get an error at taking out the Whitespaces or i do normal Strings and get an Error in the Splitting String section, also i want to display the three most repeatet Words thats why i need to have a List of all Strings.
Heres the Code:
//Reading File
var 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 String
for (int i = 0; i < text.Length; i++)
{
splitted = text.Split(',', ' ', '.', '(', ')');
list.Add(splitted);
}
//Taking out Whitespaces
for (int i = 0; i < list.Count; i++)
{
if (string.IsNullOrWhiteSpace(list[i]))
{
list.RemoveAt(i);
}
}
For every letter in that text file you will add all words. You don't need the for-loop. You also don't need the second loop because
String.Splithas an overload for that:The subtask to get the three most repeating words: