Determine List.IndexOf ignoring case [duplicate] - c#

This question already has answers here:
Case-Insensitive List Search
(8 answers)
Closed 6 years ago.
Is there a way to get the index of a item within a List with case insensitive search?
List<string> sl = new List<string>() { "a","b","c"};
int result = sl.IndexOf("B"); // should be 1 instead of -1

Try this : So there is no direct way to use IndexOf with String Comparison option for LIST, to achieve desire result you need to use Lambda expression.
int result = sl.FindIndex(x => x.Equals("B",StringComparison.OrdinalIgnoreCase));

The IndexOf method for Strings in C# has a ComparisonType argument, which should work something like this:
sl.IndexOf("yourValue", StringComparison.CurrentCultureIgnoreCase)
or
sl.IndexOf("yourValue", StringComparison.OrdinalIgnoreCase)
Documentation for this can be found here and here

Related

C#, Convert/Join List of arrays into single array [duplicate]

This question already has answers here:
Best way to combine two or more byte arrays in C#
(13 answers)
Closed 1 year ago.
I have the following list of arrays
var list = new List<string[]>();
how to use LINQ to convert/Join them into a single array
string[] singleArray;
It can be done by SelectMany operator.
var singleArray = list.SelectMany(x => x).ToArray()

How can I convert a string to an int array in C#? [duplicate]

This question already has answers here:
Convert string numbers to Array C#
(3 answers)
Closed 2 years ago.
for example suppose that I have a string like "231143" and I wanna convert it to an int[] array which I can easily access 2,3,1,1,4,3 as an int. What should I do?
Without any error checking you can do:
var value = "231143";
var array = value.Select(c => c - '0').ToArray();
This makes use of a trick whereby you can subtract '0' from the value of a single character holding a number to get its integer value.

Refactor method (C#) [duplicate]

This question already has answers here:
Simply check for multiple statements C#
(2 answers)
C# - Prettier way to compare one value against multiple values in a single line of code [duplicate]
(2 answers)
Closed 3 years ago.
I have got method:
private bool MyMethod(PlantType plantType)
{
return plantType.PlantMoveType == PlantMoveType.PlantReady
|| plantType.PlantMoveType == PlantMoveType.PlantRelase
}
Can I write it into other way? Maybe with LINQ?
One way is to put the enum values that you want to check against into an array, and call Contains.
return new[] { PlantMoveType.PlantReady, PlantMoveType.PlantRelase }
.Contains(plantType.PlantMoveType);
If you are using C# 7 or later, you can also write the method as expression-bodied:
private bool MyMethod(PlantType plantType) =>
new[] { PlantMoveType.PlantReady, PlantMoveType.PlantRelase }
.Contains(plantType.PlantMoveType);
Well a small simplification would be to pass the type (enum?) of the property PlantMoveType instead of PlantType as the parameter.
Beyond that, you could declare the types to check for as e.g. an array. In case you'd like to reuse that array, you can also declare it outside the scope of the method:
private static PlantMoveType[] _plantStates =
new []{PlantMoveType.PlantReady, PlantMoveType.PlantRelase};
private bool MyMethod(PlantMoveType plantMoveType)
{
return _plantStates.Contains(plantMoveType);
}

C# - Compare one string variables to multiple other string (String.Equals) [duplicate]

This question already has answers here:
Multiple string comparison with C#
(8 answers)
Closed 5 years ago.
Do you have an idea to avoid doing multiple String.Equals?
For instance:
if (interSubDir.Equals("de") || interSubDir.Equals("de-DE"))
Thanks!
If you are simply trying to make it more readable, or require less typing, you can write a string extension method like so:
public static class StringExt
{
public static bool EqualsAnyOf(this string value, params string[] targets)
{
return targets.Any(target => target.Equals(value));
}
}
Which you can use as follows:
if (interSubDir.EqualsAnyOf("de", "de-DE"))
Or
if (interSubDir.EqualsAnyOf("de", "de-DE", "en", "en-GB", "en-US"))
and so on.
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);
Linq could come into help for you. listToChechAgainst could be simple variable or private/public property.
var listToChechAgainst = new[] { "de", "DE-de" };
if(listToChechAgainst.Any(x => innerSubDir.Equals(x)));

Finding if UpperCase of a value exists in a list of strings [duplicate]

This question already has answers here:
How to ignore the case sensitivity in List<string>
(12 answers)
Closed 7 years ago.
I have something like this:
string configKeys = "othewr|RDX|MDX";
and wrote a code like this to see if value "OTHER" exists in that list
List<string> values = configKeys.Split('|').ToList();
var b = values.Find(item => item.Trim().ToUpper() == "OTHER").FirstOrDefault();
but for example because I have typed it wrong "othewr" so it crashes, but just want it to tell me if it exists or not as a boolean. How can I change the code to do that and not crash?
Use Any. If the predicate evaluates for at least one value in the collection it returns true, else false:
var b = values.Any(item => item.Trim().ToUpper() == "OTHER");

Categories