这段代码:
const string LabelToFind = "goTo considered Harmful";
using (var file = new StreamReader(DownloadedFile))
{
string line;
while ((line = file.ReadLine()) != null) {
if ((line.Contains(keyVal)) && (line.Contains(LabelToFind))) {
string[] logLineElements = line.Split('|');
foreach (string element in logLineElements) {
if (element.Contains(LabelToFind)) {
return element.Substring(element.IndexOf(LabelToFind, StringComparison.Ordinal) + LabelToFind.Length, element.Length - LabelToFind.Length);
}
}
}
}
}
...失败,“参数超出范围异常:索引和长度必须引用字符串中的位置。参数名称:长度”当“元素”具有前导和尾随空格时(它总是这样做)。我想我可以这样做:
foreach (string element in logLineElements) {
if (element.Contains(LabelToFind)) {
String s = element.Trim();
return s.Substring(s.IndexOf(LabelToFind, StringComparison.Ordinal) + LabelToFind.Length, s.Length - LabelToFind.Length);
……但是味道不对……