user222427
user222427

Reputation:

C# string.Contains using variable

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?

Upvotes: 0

Views: 1902

Answers (3)

Will A
Will A

Reputation: 25008

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

Upvotes: 0

tdammers
tdammers

Reputation: 20721

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.

Upvotes: 2

kbrimington
kbrimington

Reputation: 25692

Using LINQ:

if (keywords.Any(k => pslower.Contains(k)))

Upvotes: 2

Related Questions