Hello. There is a text, how to make it register in the List<List<string>> , where List<List<string>> is the Sentence, and List<string> Words?

  • 3
    What is your problem? - Andrey NOP
  • I understand how to break down the list <.string> (sentences) separately, and then list <.string> (words), is it possible to immediately translate a string into list <.list <.string >>? - Ivan
  • one
    You can: var list = text.Split('.', '!', '?').Select(sentence => sentence.Split(' ').ToList()).ToList(); - Andrey NOP
  • Many thanks - Ivan

1 answer 1

If the sentence is not overloaded with abbreviations, then this code will work. Which characters to cut from the source line - choose your own.

  // Исходная строка string source = @"В список отлей, завышающих цены, попал мини-отель без звезд «Люкс», расположенный на проспекте Королева, 1/9. Стоимость бронирования на период проведения чемпионата мира по футболу отеля на сайте Booking составляет 15 тысяч рублей, что превышает норматив на 514%."; // Убираем ненужные символы (в данном случае только запятые) string clearedSource = source.Replace(@",", ""); // Делим текст на предложения List<string> cuttedSentence = source.Split('.').ToList(); // Готовим место под финальный список List<List<string>> cuttedWords = new List<List<string>>(); // Перебираем список предложений и каждое предложение делим на слова foreach (string item in cuttedSentence) { cuttedWords.Add(item.Split(' ').ToList()); } 

The result is:

enter image description here

  • Although the solution with LINQ is more elegant. - dTi