I need to get the next line data from a text file. For example a text file abc contains:
Test Results : Fail
Error Code : 2000-0333
I need to search for keyword "Test Results : Fail" and if it exists, the data "2000-0333" should be stored in a variable.
Here's what I have done so far:
List<string> filecontents = File.ReadAllLines("abc.txt").ToList<string();
for (int i = 0; i < filecontents.Count; i++)
{
if (filecontents[i].Contains("Test Results : Fail"))
{
//need to get the data here
}
}
Could I please get solution to this?? Thank you in advance.
Your next line, will be in filecontents[i + 1]
List<string> filecontents = File.ReadAllLines("abc.txt").ToList<string();
for (int i = 0; i < filecontents.Count; i++)
{
if (filecontents[i].Contains("Test Results : Fail"))
{
string error = filecontents[i + 1];
}
}
Take care, if the last string is of the file is "Test Results : Fail", when try to get filecontents[i + 1] will throw an exception.
To get the 2000-0333, you should split the string error by the ":"
string errorNumber = filecontents[i + 1].Split(':')[1]
But, if there is no ':' it will throw an exception. Be aware of that.
You can .Trim() the errorNumber, to know for sure there is no blank space in the string.
List<string> filecontents = File.ReadAllLines("abc.txt").ToList<string();
for (int i = 0; i < filecontents.Count; i++)
{
if (filecontents[i].Contains("Test Results : Fail"))
{
string s = fileContents[i+1].Split(" : ")[1];
}
}
if( (from c in filecontents where c.Contains("Test Results : Fail") select filecontents.IndexOf(c)).Any())
{
int index = (from c in filecontents where c.Contains("Test Results : Fail") select filecontents.IndexOf(c)).SingleOrDefault();
string error = filecontents[index + 1];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.