String compareTo() Method

  • Post author:
  • Post category:Dart
  • Post comments:2 Comments
String compareTo() Method

In this guide, we will discuss String compareTo() Method in Dart Programming Language. 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 

Next Topic : Click Here

This Post Has 2 Comments

Leave a Reply