Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare String with case insensitivity, in Dart?

Tags:

dart

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']; 
like image 417
Seth Ladd Avatar asked Jul 20 '15 17:07

Seth Ladd


People also ask

How do you compare strings case insensitive?

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.

How do you compare string values in darts?

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.

How do you ignore a case in darts?

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.

Are string comparisons case sensitive?

CompareTo and Compare(String, String) methods. They all perform a case-sensitive comparison.


1 Answers

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 
like image 190
Jossef Harush Kadouri Avatar answered Oct 15 '22 05:10

Jossef Harush Kadouri