C# Case insensitive string comparison [duplicate] - c#

This question already has answers here:
Case insensitive 'Contains(string)'
(29 answers)
How to ignore the case sensitivity in List<string>
(12 answers)
Closed 8 years ago.
I have a list that holds a few strings(names). For this example.
It will hold:
TeSt1
TeSt2
TeSt3
And I'm trying to check if that list has one of those. And I'm doing this like this at the moment:
if (list.Contains(test2))
{
}
But I need it to be case insensitive.. But how can I do that? in an if statement.

The Contains method has an overload that accepts an IEqualityComparer. You can give it one by doing the following:
if (list.Contains(test2, StringComparer.OrdinalIgnoreCase))
{
// do something
}

IndexOf has a parameter for case insensitive search
culture.CompareInfo.IndexOf(toSearch, word, CompareOptions.IgnoreCase)
where culture is the instance of CultureInfo describing the language that the text is written in.
You can loop through the list and see if it each list entry matches the search.

Make your list lower case......and
if (list.Contains(test2.ToLower()))
{
}

Related

How to compare case insensitive string using FluentAssertions? C# [duplicate]

This question already has answers here:
Can Fluent Assertions use a string-insensitive comparison for IEnumerable<string>?
(4 answers)
Closed 5 years ago.
How can I easy compare string case insensitive using FluentAssertions?
Something like:
symbol.Should().Be(expectedSymbol, StringComparison.InvariantCultureIgnoreCase);
Edit: Regarding possible duplicate and code:
symbol.Should().BeEquivalentTo(expectedSymbol);
it is comparing using CurrentCulture. And it will brake in situation like Turkish culture. Where
Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR", false);
string upper = "in".ToUpper(); // upper == "İN"
"in".Should().BeEquivalentTo("In"); // It will fail
so the part "StringComparison.InvariantCultureIgnoreCase" is crucial here.
You can use
symbol.ToLower().Should().Be(expectedSymbol.ToLower());
OR
Instead of Be use BeEquivalentTo
symbol.Should().BeEquivalentTo(expectedSymbol);
BeEquivalentTo metadata states
Asserts that a string is exactly the same as another string, including any leading or trailing whitespace, with the exception of the casing.

How do i determinate if a sentence has digits/numbers, and then assign those numbers to variables? [duplicate]

This question already has answers here:
Find and extract a number from a string
(32 answers)
Closed 7 years ago.
I already tried to determinate the digits in a sentence using 'isDigit', but this gives me a 'bool' output. I need an 'int' output.
What i want to do is, say, i have the sentence "cheese23"; "2" and "3" will be put in their own variable, so i can add/subtract/multiply/ etc them.
(x=2,y=3;)
help will be hugely appreciated (self-teaching beginner here)
Do:
int[] intArray = "Cheese23".Where(Char.IsDigit).Select(c => int.Parse(c.ToString())).ToArray();
This extracts the numbers in the string in the order and creates an array out of it.
Then you can do intArray[0] to get 2 and intArray[1] to get 3.
Search up LINQ to see how those chain of methods did it.

Query a list of objects that contains any string [duplicate]

This question already has answers here:
Check if a string contains an element from a list (of strings)
(12 answers)
Closed 8 years ago.
I'm this expression to search inside a list of objects by a specific property:
var result = myObject.Where(o => o.SearchString.Contains(searchValue));
It works good for a single value. The searchValue is a string passed by the user. The user can pass a single word or many words separeted by spaces. Is there any way to filter the objects that contains any of the passed words?
I could do this with a loop, searching a new word in previous results, but it doesn't seem very elegant.
myObject.Where(o => words.Any(o.SearchString.Contains))

How to compare strings at C# like in SQL server case insensitive and accent insensitive [duplicate]

This question already has answers here:
How to compare strings with case insensitive and accent insensitive
(2 answers)
How can I do a case insensitive string comparison?
(9 answers)
Closed 8 years ago.
Alright here 4 words which are equal at SQL server Latin1_General_100_CI_AI collation which is case insensitive and accent insensitive
taraflı
TaraFLI
TARaFLİ
Tarafli
However i could not find a way to compare these as equal at C# .net 4.5.1
Are there any way to make such comparsion like in SQL server ?
I checked this thread as well : Ignoring accented letters in string comparison
Event RemoveDiacritics method at that thread fails
This compares all those strings as equal:
string.Compare(s1,s2,
CultureInfo.InvariantCulture,
CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase)

Find string in text with linq regardless of case or culture [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Ignoring accented letters in string comparison
I have a search textbox that searches the given text in all the news from the database. So I have this:
List<NewsTranslation> newsTranslations = GetByLanguage(GlobalBL.CultureLanguage);
return newsTranslations.Where(
e =>
e.NewsContent.Contains(searchText) || e.NewsDescription.Contains(searchText) ||
e.NewsTitle.Contains(searchText)).ToList();
which works fine, but I would need it not to consider case or accents on letters.
Thanks
This comparator:
string.Compare(searchText, e, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase));
will do the trick. You can include this in your LINQ query where it will of course return 0 when the two arguments are equivalent.

Categories