0

i have the following

string[] statelookup = ConfigurationManager.AppSettings["WstmStateLookUp"].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

with these values

"AL, AR, CT, MD, DE, FL, GA, IA, IL, IN, KY, LA, MA, ME, MI, MN, MO, MS, NC, NH, NJ, NY, OH, PA, RI, SC, TN, VA, VT, WI, WV"

I need to see if any line from a file contains one of the above values. A sample line is this

<FIPS>20170<STATE>AL<WFO>AJK

When I use the following, I only get results for whatever is the first item in the statelookup (in this case, AL).

statelookup.Any(a => currentLine.Contains("<STATE>"+a))

i can move items around (say have OH first and ill get the OH result) but I cant seem to be able to return all the lines form the file that contain ANY of the statelookup values.

I have to use

"<STATE"+a 

because AL, AK, and RI can appear in other lines of the file. this way I only look for this spcific instance.

What am i missing?

4
  • 1
    I don't think your problem is in the code you've shown. Assuming you have the list of lines, lines.Where(currentLine -> statelookup.Any(a => currentLine.Contains("<STATE>"+a))).ToList() should work. Commented Aug 30, 2017 at 22:08
  • 1
    I you're splitting on ',' (comma) and they are split by ', ' (comma space) then you might be getting spaces in your string collection. Commented Aug 30, 2017 at 22:13
  • As @user1304444 mentioned, remove the spaces in between commas and states. Anytime you allow list configurations like this, your code should account for meaningless characters such as spaces. Commented Aug 30, 2017 at 22:24
  • Your question is not clear: Why would it return anything else except for AL? This line: <FIPS>20170<STATE>AL<WFO>AJK only contains <STATE>AL so why should it return any other state? Commented Aug 30, 2017 at 22:33

2 Answers 2

2

The problem is that your array does not contain what you think it does... IF you split "AL, AR, CT" on commas then you get {"AL"," AR"," CT"}. As you can see every entry except the first has a leading space which is causing your match to not work.

The three options you have are to either store your string without the spaces, split on ", " or better to strip whitespace using the Trim method after the split before putting them into the array. The last is my preferred because it works if you have double or triple spaces (or more) for some reason.

Something like this would work:

string[] statelookup = ConfigurationManager.AppSettings["WstmStateLookUp"].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x=>x.Trim()).ToArray();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Chris. I am an idiot lol. I stared at this code for 2-3 hours yesterday. I should have known it was a stupid space. it always is. I read that line so many times and it never registered. This is what worked.
0

As mentioned by @user1304444 in the comments, you are splitting the array incorrectly. I've made a working version of the code here:

https://dotnetfiddle.net/pFO6iV

You will notice that the state AL can be found, since there is no preceding space and it therefore becomes simply "AL" in the string array.

I've removed the spaces using string.Replace() but you can do it in many ways.

string[] statelookup ConfigurationManager
    .AppSettings["WstmStateLookUp"]
    .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.