String compareTo() Method


Advertisements

Returns a new string by removing all leading and trailing spaces. However, this method doesn’t discard spaces between two strings.

Syntax

compareTo(String other)

Return Type

Returns an integer representing the relationship between two strings.

  • 0 − when the strings are equal.

  • 1 − when the first string is greater than the second

  • -1 − when the first string is smaller than the second

Example

void main() { 
   String str1 = "A"; 
   String str2 = "A"; 
   String str3 = "B"; 
   
   print("str1.compareTo(str2): ${str1.compareTo(str2)}"); 
   print("str1.compareTo(str3): ${str1.compareTo(str3)}"); 
   print("str3.compareTo(str2): ${str3.compareTo(str2)}"); 
} 

It will produce the following output −.

str1.compareTo(str2): 0 
str1.compareTo(str3): -1 
str3.compareTo(str2): 1 
dart_programming_string.htm
Advertisements