string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Admin\Desktop\ID.csv");

string sPattern = idBox.Text;

foreach (string s in lines)
{
    if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
    {
        nameLabel.Text = "test success";
    }
    else
    {
        nameLabel.Text = "test failure";
    }
}

This is basically what I have so far. I am using Visual C#, which is probably obvious, but just in case you didn't know.

ID.csv includes:
123456a, 123457b, 123458c

I want to separate the IDs into separate lines, and be able to search within the string.
If someone enters 456, I would like nameLabel.Text to output the entire line from within the string[]. I know i will need line.Split(',') somewhere, I've read that multiple places but am not sure on how to implement it. Also, it always outputs "test failure" to the nameLabel no matter if what i entered was correct or not. Any help would be appreciated.

Recommended Answers

All 4 Replies

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?

i would ONLY want 123456a

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<string> if you're familiar with that to make it easier to search.

...point of clarity
I'm a linq freak, so sometimes I mention shortcuts that require a litle more explanation.

I would load a List<string> 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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.