【发布时间】:2013-05-13 17:50:17
【问题描述】:
System.IO.StreamReader file = new System.IO.StreamReader(@"data.txt");
List<String> Spec= new List<String>();
while (file.EndOfStream != true)
{
string s = file.ReadLine();
Match m = Regex.Match(s, "Spec\\s");
if (m.Success)
{
int a = Convert.ToInt16(s.Length);
a = a - 5;
string part = s.Substring(5, a);
Spec.Add(part);
}
}
我正在尝试获取包含单词“Spec”和空格字符的所有行,但运行此程序时出现错误。
异常详情如下:
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
谁能帮我找出原因?
文本文件:
ID 560
Spec This ... bla bla
blah...
blah...
bla bla
bla
Category Other
Price $259.95
ID 561
Spec more blah blah...
blah...
blah...
bla bla
bla
Category Other
Price $229.95
【问题讨论】:
-
什么错误?
OutOfMemoryException?InvalidCastException?什么? -
别管什么错误,你试过调试吗?
-
我建议添加文件示例以及您想从中获得什么。请注意,SO
int a = Convert.ToInt16(s.Length);中最搞笑的代码行之一。 -
子字符串关闭,然后。你能给我们看一段数据列表的摘录吗?此外,File.ReadAllLines >>>> StreamReader + 手册。