You might not need to split it if you're using the entire line as output -- just use Contains()
...or are you saying you would ONLY want 123456a if someone enters 456?
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
Then yes, just call Split at the end of the ReadAllLines() using commas as the delimiter. Then you can just search the array.
You can also convert that to a List if you're familiar with that to make it easier to search.
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
...point of clarity
I'm a linq freak, so sometimes I mention shortcuts that require a litle more explanation.
I would load a List of the items in the CSV like this:
//using System.Linq;
//private static List<string> lst_strKeys = new List<string>(); //possibly at class level
private static bool LoadFileGetLines(List<string> lst_strLines, string strFileName, ref string strError)
{
bool blnRetVal = true;
try
{
System.IO.File.ReadAllLines(strFileName).ToList().ForEach(str =>
str.Split(',').ToList().ForEach(s => lst_strLines.Add(s.Trim())));
}
catch (Exception exc)
{
blnRetVal = false;
strError = exc.Message;
}
return blnRetVal;
}
I would load that outside of where the user makes a choice so the file is not loaded on every choice (like on the page load).
if (!LoadFileGetLines(lst_strKeys, "../../ID.csv", ref strError))
{
System.Diagnostics.Debug.WriteLine("Could not load input file: " + strError);
// You can display that error to the user or take whatever necessary action.
return;
}
To then get data from that list based on substrings, I would do this:
//strUserText comes from your user control (textbox or other)
string strFoundData = lst_strKeys.Where(s => s.Contains(strUserText)).FirstOrDefault();
//then evaluate if it was actually found and mention it to the user
if (!string.IsNullOrEmpty(strFoundData))
{
nameLabel.Text = strFoundData;
}
else
{
nameLabel.Text = "not found";
}
If this is confusing, please let me know.
The .csv file I used was this:
123456a, 123457b, 123458c
123556a, 123557b, 123558c
123656a, 123657b, 123658c
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402