В строке с кодом "string str3 = text.Substring(pos, pos2);" выскакивает ошибка - C#

Узнай цену своей работы

Формулировка задачи:

Доброго всем вечера! Есть текстовый документ в нем следующие строки: a Сегодня плохая погода. end b Сегодня хорошая погода. end c Солнце на улице, пойду гулять! end И пользователь должен ввести только b в консоли а ему выводится на экран "Сегодня хорошая погода." Общими усилиями форумчанинов, наклипали код который находит это дело должен провернуть:
Листинг программы
  1. static void Main(string[] args)
  2. {
  3. string str = String.Empty;
  4. string path = @"c:\1.txt";
  5. StreamReader reader = new StreamReader(path, Encoding.GetEncoding(1251));
  6. str = reader.ReadToEnd();
  7. Console.Write("Введите слово: ");
  8. string word = Console.ReadLine();
  9. int index = str.IndexOf(word, StringComparison.CurrentCulture);
  10. if (index > -1)
  11. {
  12. index += word.Length;
  13. FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
  14. {
  15. fs.Seek(index, SeekOrigin.Begin);
  16. byte[] result = new byte[fs.Length - index];
  17. fs.Read(result, 0, result.Length);
  18. string text = Convert.ToString(result);
  19. string s2 = word;
  20. string s = "end";
  21. text.Contains(s2);
  22. int pos = text.IndexOf(s2);
  23. text.Contains(s);
  24. int pos2 = text.IndexOf(s);
  25. Console.Write(pos2);
  26. string str3 = text.Substring(pos, pos2);
  27. Console.WriteLine(str3);
  28. Console.ReadLine();
  29. }
  30. }
  31. else
  32. Console.WriteLine("Нет такого слова.");
  33. Console.ReadKey();
  34. }
С точки зрения логики все должно работать как часы, но в этом месте "string str3 = text.Substring(pos, pos2);" выскакивает ошибка! Просветите, может я чего не так сделал...Спасибо!

Решение задачи: «В строке с кодом "string str3 = text.Substring(pos, pos2);" выскакивает ошибка»

textual
Листинг программы
  1. static void Main(string[] args)
  2.         {
  3.             Console.Title = "test";
  4.             string str = String.Empty;
  5.             string path = @"c:\2.txt";
  6.             StreamReader reader = new StreamReader(path, Encoding.GetEncoding(1251));
  7.             str = reader.ReadToEnd();
  8.             Console.Write("Введите слово: ");
  9.             string word = Console.ReadLine();
  10.             int index = str.IndexOf(word, StringComparison.CurrentCulture);
  11.             if (index > -1)
  12.             {
  13.                 index += word.Length;
  14.                 FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
  15.                 {
  16.                     fs.Seek(index, SeekOrigin.Begin);
  17.                     byte[] result = new byte[fs.Length - index];
  18.                     fs.Read(result, 0, result.Length);
  19.              string text = word + Encoding.GetEncoding(1251).GetString(result);
  20.              string s2 = word;
  21.              string s = "end";
  22.              bool a = text.Contains(s2);
  23.              int pos = text.IndexOf(s2);
  24.              bool b = text.Contains(s);
  25.              int pos2 = text.IndexOf(s);
  26.              string str3 = text.Substring(pos, pos2);
  27.              Console.WriteLine(str3);
  28.              Console.ReadLine();
  29.                 }
  30.             }
  31.             else
  32.                 Console.WriteLine("Нет такого слова.");
  33.                 Console.ReadKey();
  34.         }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 4.273 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы