Multiple string comparison with C#
Use Enumerable.Contains<T>
which is an extension method on IEnumerable<T>
:
var strings = new List<string> { "A", "B", "C" };
string x = // some string
bool contains = strings.Contains(x, StringComparer.OrdinalIgnoreCase);
if(contains) {
// do something
}
C# - Compare one string variables to multiple other string (String.Equals)
Create collection of values:
string[] values = { "de", "de-DE" };
Use Contains
method:
if (values.Contains(interSubDir))
It gives O(n) performance.
If your collection is very big, then you can use Array.BinarySearch
method, that gives you O(log n) performance.
if (Array.BinarySearch(values, interSubDir) >= 0)
However, the collection must be sorted first.
Array.Sort(values);
Compare multiple strings in C#
Assuming that you've stored the strings in a colllection like array or list, you can use Enumerable.All
:
string first = strings.FirstOrDefault(s => s != "N/A");
bool allEqual = first == null || strings.All(s => s == "N/A" || s == first);
Explanation: you can compare all strings with one of your choice(i take the first), if one is different allEqual
must be false. I need to use FirstOrDefault
since it's possible that all strings are "N/A" or the list is empty, then First
would throw an exception.
DEMO
C# How to compare multiple array values (strings) in if-statement?
In c#, you can't use
if (a == b == c)
Instead you have to be explicit:
if (a == b && b == c)
So instead of
board[0].Equals(board[1]).Equals(board[2]).Equals("X")
You have to write
board[0] == "X" && board[1] == "X" && board[2] == "X"
Compare multiple strings to multiple regular expressions C#
You can use this code:
public List<string> ValidateNames(List<string> inputList, List<string> patternList)
{
var missMatchInputList = new List<string>();
foreach (var input in inputList)
foreach (var pattern in patternList)
if (!Regex.IsMatch(input, pattern))
{
missMatchInputList.Add(input);
break;
}
return missMatchInputList;
}
C# Multiple String Compare
If those strings are the same for all the cases - make them global:
string a = "test";
string b = "try";
string c = "compare";
//case 1
for (int i = 1; i <= 2; i++)
{
if (a == b) { do something };
if (a == c) { do something};
}
//case 2
for (int i = 3; i <= 4; i++)
{
if (a == b) { do something };
if (a == c) { do something};
}
Doing this you will avoid repeating initializing this strings in every case
Edit 1
you can create function to select values:
for (int i = 1; i <= 2; i++)
{
bw.Write(SelectDataForBinaryWriter("A"));
}
...
private int SelectDataForBinaryWriter(string input)
{
int output = 0;
switch (input) // edit your own cases here
{
case "A":
output = 0x123;
break;
case "B":
output = 0x456;
break;
case "C":
output = 0x789;
break;
default:
output = some_default_value_here;
break;
}
return output;
}
As I can see, you are writing hex values, so you can also consider returning already parsed value - C# convert integer to hex and back again
String, c# how to compare string a same time
You can, for example, create an array then check if it contains what you search for:
if(new[] { "MS", "VI", "PS" }.Contains(MirePrincipal.Substring(0, 2)))
Related Topics
Entity Framework Datetime and Utc
What Are the Benefits to Marking a Field as 'Readonly' in C#
Automatically Inotifypropertychanged
Redirecting Unauthorized Controller in ASP.NET MVC
Soap Client in .Net - References or Examples
How to Upload Files in ASP.NET Core
How to Get the CPU Temperature
Produce a Random Number in a Range Using C#
How to Send HTML-Formatted Email
How to Fast Get Hardware-Id in C#
Modelstate.Isvalid == False, Why
Windows Service to Run Constantly
How to Redirecttoaction in ASP.NET MVC Without Losing Request Data
Sending Email with Attachments from C#, Attachments Arrive as Part 1.2 in Thunderbird