How do I compare two string in Dart, with case insensitivity?
For example, I have this list:
var list = ['Banana', 'apple'];
And I would like to sort it such that apple
is before Banana
.
End result:
['apple', 'Banana'];
The equalsIgnoreCase() method of the String class is similar to the equals() method the difference if this method compares the given string to the current one ignoring case.
You can use compareTo to compare strings. String rubi = 'good'; String ore = 'good'; rubi. compareTo(ore) == 0; You need to check for NULL values though.
There's no built in way to compare strings case-insensitive in dart (as @lrn answered). Show activity on this post. There is no case-insensitive string compare function (or string equality function for that matter) in Dart.
CompareTo and Compare(String, String) methods. They all perform a case-sensitive comparison.
There's no built in way to compare strings case-insensitive in dart (as @lrn answered).
If you only want to compare strings case-insensitive, I would go with declaring the following method somewhere in a common place:
bool equalsIgnoreCase(String string1, String string2) { return string1?.toLowerCase() == string2?.toLowerCase(); }
Example:
equalsIgnoreCase("ABC", "abc"); // -> true equalsIgnoreCase("123" "abc"); // -> false equalsIgnoreCase(null, "abc"); // -> false equalsIgnoreCase(null, null); // -> true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With