I have a string of words separated by spaces. How to split the string into lists of words based on the words length?
Example
inpu
A bit lengthy solution but does get the result in a Dictionary
class Program
{
public static void Main()
{
Print();
Console.ReadKey();
}
private static void Print()
{
GetListOfWordsByLength();
foreach (var list in WordSortedDictionary)
{
list.Value.ForEach(i => { Console.Write(i + ","); });
Console.WriteLine();
}
}
private static void GetListOfWordsByLength()
{
string input = " aa aaa aaaa bb bbb bbbb cc ccc cccc ";
string[] inputSplitted = input.Split(' ');
inputSplitted.ToList().ForEach(AddToList);
}
static readonly SortedDictionary> WordSortedDictionary = new SortedDictionary>();
private static void AddToList(string s)
{
if (s.Length > 0)
{
if (WordSortedDictionary.ContainsKey(s.Length))
{
List list = WordSortedDictionary[s.Length];
list.Add(s);
}
else
{
WordSortedDictionary.Add(s.Length, new List {s});
}
}
}
}