Index was out of range - не понемаю как такое возмозно - C#

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

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

Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. namespace lab2_2
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. try
  13. {
  14. string f1;
  15. string f2;
  16. Console.WriteLine("enter file location for modification:");
  17. f1 = Console.ReadLine();
  18. string[] str = File.ReadAllLines(f1);
  19. Console.WriteLine("enter name and path output file");
  20. f2 = Console.ReadLine();
  21.  
  22. int b;
  23. int len = str.Length;
  24. Console.WriteLine(len);
  25. using (StreamWriter sw = File.CreateText(f2))
  26. {
  27. sw.AutoFlush = true;
  28. for (int i = 2; i <len-1; i++)
  29. {
  30. b = str[i].IndexOf(";");
  31. str[i].Insert(b,"\n"); // здеся не канает
  32. sw.WriteLine(str[i]);
  33. }
  34. }
  35. Console.WriteLine("all coments erase!");
  36. Console.ReadLine();
  37. }
  38. catch (FileNotFoundException e)
  39. {
  40. Console.WriteLine("File not found! " + e.Message);
  41. Console.ReadKey();
  42. }
  43. }
  44. }
  45. }
подскажите что не так то?

Решение задачи: «Index was out of range - не понемаю как такое возмозно»

textual
Листинг программы
  1.                 if (str[i].Contains(";")) {
  2.                     str[i].Insert(str[i].IndexOf(";"), "\n");
  3.                     Console.Write(str[i] + "\n");
  4.                 }

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


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

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

13   голосов , оценка 4.154 из 5

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

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

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