В строке с кодом "string str3 = text.Substring(pos, pos2);" выскакивает ошибка - C#
Формулировка задачи:
Доброго всем вечера! Есть текстовый документ в нем следующие строки:
a Сегодня плохая погода. end
b Сегодня хорошая погода. end
c Солнце на улице, пойду гулять! end
И пользователь должен ввести только b в консоли а ему выводится на экран "Сегодня хорошая погода."
Общими усилиями форумчанинов, наклипали код который находит это дело должен провернуть:
С точки зрения логики все должно работать как часы, но в этом месте "string str3 = text.Substring(pos, pos2);" выскакивает ошибка! Просветите, может я чего не так сделал...Спасибо!
static void Main(string[] args)
{
string str = String.Empty;
string path = @"c:\1.txt";
StreamReader reader = new StreamReader(path, Encoding.GetEncoding(1251));
str = reader.ReadToEnd();
Console.Write("Введите слово: ");
string word = Console.ReadLine();
int index = str.IndexOf(word, StringComparison.CurrentCulture);
if (index > -1)
{
index += word.Length;
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
{
fs.Seek(index, SeekOrigin.Begin);
byte[] result = new byte[fs.Length - index];
fs.Read(result, 0, result.Length);
string text = Convert.ToString(result);
string s2 = word;
string s = "end";
text.Contains(s2);
int pos = text.IndexOf(s2);
text.Contains(s);
int pos2 = text.IndexOf(s);
Console.Write(pos2);
string str3 = text.Substring(pos, pos2);
Console.WriteLine(str3);
Console.ReadLine();
}
}
else
Console.WriteLine("Нет такого слова.");
Console.ReadKey();
}Решение задачи: «В строке с кодом "string str3 = text.Substring(pos, pos2);" выскакивает ошибка»
textual
Листинг программы
static void Main(string[] args)
{
Console.Title = "test";
string str = String.Empty;
string path = @"c:\2.txt";
StreamReader reader = new StreamReader(path, Encoding.GetEncoding(1251));
str = reader.ReadToEnd();
Console.Write("Введите слово: ");
string word = Console.ReadLine();
int index = str.IndexOf(word, StringComparison.CurrentCulture);
if (index > -1)
{
index += word.Length;
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
{
fs.Seek(index, SeekOrigin.Begin);
byte[] result = new byte[fs.Length - index];
fs.Read(result, 0, result.Length);
string text = word + Encoding.GetEncoding(1251).GetString(result);
string s2 = word;
string s = "end";
bool a = text.Contains(s2);
int pos = text.IndexOf(s2);
bool b = text.Contains(s);
int pos2 = text.IndexOf(s);
string str3 = text.Substring(pos, pos2);
Console.WriteLine(str3);
Console.ReadLine();
}
}
else
Console.WriteLine("Нет такого слова.");
Console.ReadKey();
}