Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

259
Views
Check if none of given strings contains a certain string

I have a list of variables declared for a string value. It is from an external source, there could be 5-10 variables. How do I check if all of them do not contain a certain string ("K.K")? I do not want to check for each such as

public string string_1;
public string string_2;
public string string_3;
...
...
...

if(string_1!= "K.K") //for all the strings for non-array collection
{}
...
...
...
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

If you know how do get your variables into an array/List

var strings = new List<string>();
strings.Add(string_1);
strings.Add(string_2);
strings.Add(string_3);
...

or

var strings = new List<string>{string_1, string_2, string_3, ...};

or

var strings = new []{string_1, string_2, string_3, ...};

you could e.g. use Linq Any and as filter string.Contains or also string.Equals depending on your needs

if(!strings.Any(s => s.Contains("K.K")))
{
    ...
}

Alternatively to using Equals if you know your strings should all be unique you could also use

var strings = new HashSet<string>();
strings.Add(string_1);
strings.Add(string_2);
strings.Add(string_3);
...

or

var strings = new HashSet<string>{string_1, string_2, string_3, ...};

and then simply check

if(!strings.Contains("K.K"))
{
    ...
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!