Skip to content

ErcanOPAK.com

  • ASP.Net WebForms
  • ASP.Net MVC
  • C#
  • SQL
  • MySQL
  • PHP
  • Devexpress
  • Reportviewer
  • About
C#

Determine If string contains more than 1 value in C#

- 16.03.20 | 29.03.20 - ErcanOPAK

Let’s say you have a condition like that:

if ((myString.Contains("Value1") && (myString.Contains("Value2")) && (myString.Contains("Value3")))    
{    
...
}

Basically, you want to check if all of your values are contained in the string . Fortunately (with the help of LINQ), this can by translated almost literally into C#:

var values = new String[] {"Value1", "Value2", "Value3"};

if (values.All(v => myString.Contains(v))) 
{
    ...
}

Similarly, if you want to check if any value is contained in the string, you’d substitute All by Any.

if ((myString.Contains("Value1") || (myString.Contains("Value2")) || (myString.Contains("Value3")))    
{
...    
}
//You can translate it to this:
if (values.Any(v => myString.Contains(v))) 
{
    ...
}

Related posts:

How to use Stopwatch in C#

What is the difference between HashSet and List in .net?

What is the difference between IQueryable and IEnumerable?

How to use and get HiddenField Value in Gridview

Post Views: 31

Post navigation

How to calculate the sum of the datatable column in asp.net?
What does the word ‘new’ mean exactly in C#?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

January 2025
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  
« Oct    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (871)
  • Get the First and Last Word from a String or Sentence in SQL (765)
  • How to select distinct rows in a datatable in C# (727)
  • How to add default value for Entity Framework migrations for DateTime and Bool (616)
  • Add Constraint to SQL Table to ensure email contains @ (529)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (497)
  • How to make theater mode the default for Youtube (491)
  • Average of all values in a column that are not zero in SQL (467)
  • Find numbers with more than two decimal places in SQL (391)
  • How to check for ‘IS NOT NULL’ And ‘IS NOT EMPTY’ string in SQL (382)

Recent Posts

  • How to Reset Taskbar in Windows 11
  • Essential Steps to Take After Windows 11 Updates
  • How to list all tables referencing a table by Foreign Key in MS SQL
  • How to format date in Javascript
  • How to generate a random number for each row in T-SQL
  • How to solve ‘Microsoft.TeamFoundation.Git.Contracts.GitCheckoutConflictException’ problem
  • Why nautical mile equals 1852 mt
  • How to Find Day Name From Date in SQL Server
  • How to make pagination in MS SQL Server
  • How to update Identity Column in SQL Server

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (871)
  • Get the First and Last Word from a String or Sentence in SQL (765)
  • How to select distinct rows in a datatable in C# (727)
  • How to add default value for Entity Framework migrations for DateTime and Bool (616)
  • Add Constraint to SQL Table to ensure email contains @ (529)

Recent Posts

  • How to Reset Taskbar in Windows 11
  • Essential Steps to Take After Windows 11 Updates
  • How to list all tables referencing a table by Foreign Key in MS SQL
  • How to format date in Javascript
  • How to generate a random number for each row in T-SQL

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter

© 2025 ErcanOPAK.com

Proudly powered by WordPress | Theme: Xblog Plus by wpthemespace.com