0
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 pslower.Contains(keywords) but I'm not trying to do 100 foreach loops.

Does anybody have any suggestions?

3 Answers 3

2

Using LINQ:

if (keywords.Any(k => pslower.Contains(k)))
Sign up to request clarification or add additional context in comments.

4 Comments

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

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.

Comments

0

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'.

Comments

Your Answer

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