This code:
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);
}
}
}
}
}
…fails with “Argument Out of Range Exception: Index and length must refer to a location within the string.
Parameter name: length” when “element” has a leading and trailing space (which it always does). I guess I could do this:
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);
…but it doesn’t smell right…
You can split and trim same time before the foreach.