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

Useful Video Courses


Video

Dart tutorial for Beginners

44 Lectures 4.5 hours

Sriyank Siddhartha

Video

Flutter Tutorial for Beginners with Dart

34 Lectures 4 hours

Sriyank Siddhartha

Video

Dart Masterclass Programming: iOS/Android Bible

69 Lectures 4 hours

Frahaan Hussain

Video

Flutter & Dart Development For Building iOS and Android Apps

117 Lectures 10 hours

Frahaan Hussain

Video

COMPLETE Google DART Programming- BOOTCAMP

22 Lectures 1.5 hours

Pranjal Srivastava

Video

AWS Mobile with Google Dart

34 Lectures 3 hours

Pranjal Srivastava

Advertisements