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.
Related
This question already has answers here:
Regular expression to match characters at beginning of line only
(8 answers)
Closed 7 years ago.
I have a string of type "12.43 some non important stuff", I want to get the number 12.43, but only if it is at exact position, which is 0 in this case.
"\d+" will return 12.43, but it will return it even in case of "hello world 12.43 some non important stuff".
Is there an easy way (maybe not regex even) to get the decimal only if it's on desired position?
You can use ^ (start anchor)
^\d+
If you know a specific position, (say 5th) you can do the following:
^.{4}(\d+)
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)
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()))
{
}
This question already has answers here:
Difference between InvariantCulture and Ordinal string comparison
(9 answers)
Closed 8 years ago.
What is the difference between
This
var ext = name.LastIndexOf(#".");
and This
var ext = name.LastIndexOf(#".", System.StringComparison.Ordinal);
From the StringComparison enum documentation on MSDN:
An operation that uses ordinal sort rules performs a comparison based on the numeric value (Unicode code point) of each Char in the string. An ordinal comparison is fast but culture-insensitive. When you use ordinal sort rules to sort strings that start with Unicode characters (U+), the string U+xxxx comes before the string U+yyyy if the value of xxxx is numerically less than yyyy.
The extra parameter is telling the method how to compare strings. With Ordinal, it's going to use unicode code points for the comparison. Other values of the enum use the culture (invariant or the current one) and can use case insensitive comparison.
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.