String compareTo() 方法



刪除所有前導和尾隨的空格,返回一個新的字串。但是,該方法不會丟棄兩個字串之間的空格。

語法

compareTo(String other)

返回型別

返回一個整數,表示兩個字串之間的關係。

  • 0 - 當兩個字串相等時。

  • 1 - 當第一個字串大於第二個字串時

  • -1 - 當第一個字串小於第二個字串時

示例

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)}"); 
} 

它將產生以下輸出 -.

str1.compareTo(str2): 0 
str1.compareTo(str3): -1 
str3.compareTo(str2): 1 
dart_programming_string.htm
廣告
© . All rights reserved.