2

I have a list of strings and i have multiple strings added to it . ex: l1 is a list with three values

List<string> l1 = new List<string> 
{
     "issue: there are multiple data errors ",
     "resolution: you can correct the data or /n raise an SR on this issue",
     "rootfix: you need to be contious while entering the data "
};

Now i need to check if this list has got all three key strings ie.

List<string> keyWords = new List<string> { "isue", "resolution", "rootfix" };

I dont think i can use Contains operation on this as the above strings are substrings of the list strings.

Please let me know if there is any function available /logic.

If all the three substrings are there in the list then i need to return boolian

11
  • 1
    You can use StartsWith()-> learn.microsoft.com/en-us/dotnet/api/… Commented Jan 7, 2020 at 14:08
  • 2
    What's wrong with using Contains? You can just use it in a loop Commented Jan 7, 2020 at 14:13
  • 2
    Or use List<T>.Any() Commented Jan 7, 2020 at 14:14
  • 1
    @Aousafrashid contains needs full string to check . but i have to check with only substrings and there are multiple substrings Commented Jan 7, 2020 at 14:16
  • 1
    "I dont think i can use contains operation" you can but not the IEnumerable.Contains method but the String.Contains method Commented Jan 7, 2020 at 14:20

4 Answers 4

2

You can check whether All keywords match in Any item of your wordset that might String.Contains your key word

List<string> keyWords = new List<string>{"issue","resolution","rootfix"};

List<string> wordSet = new List<string>{"issue: there are multiple data errors ", "resolution: you can correct the data or /n raise an SR on this issue", "rootfix: you need to be contious while entering the data "};

bool result = keyWords.All(x => wordSet.Any(w => w.Contains(x)));

For a human being it is probably easier to read from inside out

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not 100% clear on whether your example is showing three separate strings in your List<string>, or whether they are one single string.

In the first case, three separate strings in a List<string>, you could use something like:

bool isInList = l1.Any(x => x.StartsWith("issue")) &&
                l1.Any(x => x.StartsWith("resolution")) &&
                l1.Any(x => x.StartsWith("rootfix"));

This will return true if all of the matching strings are found in l1.

If it is the second case, and a single string containing the values as you described them:

string l1 = "{\"issue: there are multiple data errors \", \"resolution: you can correct the data or /n raise an SR on this issue\", \"rootfix: you need to be contious while entering the data \"}";
bool isInString = l1.Contains("\"issue") &&
                  l1.Contains("\"resolution") &&
                  l1.Contains("\"rootfix"))

This will return true where all three names appear in the same string.

Comments

0

This should work:

var data = new []{"issue: there are multiple data errors ", "resolution: you can correct the data or /n raise an SR on this issue", "rootfix: you need to be contious while entering the data "};

bool flag = (
    data.Any(d => d.StartsWith("issue:")) &&
    data.Any(d => d.StartsWith("resolution:")) &&
    data.Any(d => d.StartsWith("rootfix:"))
    );

Comments

0

Can you use LINQ? Just using Where() and String.StartsWith() you can achieve this!

        var list = new List<string> {
            "issue: test1",
            "resolution: test2",
            "rootfix: test3"
        };

        var issues = list.Where(x => x.StartsWith("issue"));

        foreach(var i in issues) {
            Console.WriteLine(i);
        }

        Console.Read();

OUTPUT: "issue: test1"

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.