views:

28

answers:

3
string[] pullspec = File.ReadAllLines(@"C:\fixedlist.txt");
        foreach (string ps in pullspec)
        {
            string pslower = ps.ToLower();
            string[] pslowersplit = pslower.Split('|');
            var keywords = File.ReadAllLines(@"C:\crawl\keywords.txt");
            if (pslower.Contains("|"))
                {
                    if (pslower.Contains(keywords))
                    {

                        File.AppendAllText(@"C:\" + keyword + ".txt", pslowersplit[1] + "|" + pslowersplit[0] + "\n");
                    }

This doesn't compile because of if (pslower.Contains(keywords) but i'm not trying to do 100 foreach loops, doesnt anybody have any suggestions?

+2  A: 

Using LINQ:

if (keywords.Any(k => pslower.Contains(k)))
kbrimington
so how do i do File.AppendAllText(@"C:\" + keyword + ".txt", pslowersplit[1] + "|" + pslowersplit[0] + "\n");
Mike
make the selected keyword the name of the text file
Mike
@Mike: Do you mean to create a file for only the first match, or for all matches?
kbrimington
for only the matching k
Mike
@Mike: try this: var keyword = keywords.FirstOrDefault(k => pslower.Contains(k)); if (keyword != null) { /* ... */ }
kbrimington
+2  A: 

You have a collection of keywords, and you want to see if any of them (or all of them?) are contained in a given string. I don't see how you would solve this without using a loop somewhere, either explicit or hidden in some function or linq expression.

tdammers
A: 

Another solution - create a String[]of the keywords and then string[] parts = pslower.Split(yourStringArray, StringSplitOptions.None); - if any of your strings appear then parts.Length > 1. You won't easily get your hands on the keywords this way, tho'.

Will A