簡體   English   中英

String.Compare()2個字符串

[英]String.Compare() 2 Strings

我正在學習(閱讀Essential C#6第5版) String.Compare()方法,我讀到如果我在比較時讓我說1個字符串text1和1個字符串text2我得到一個數字:

// 0 if equal
// negative if text1 < text2
// positive if text1 > text2

所以當我這樣做

string text1 = "Hello";
string text2 = "Hello";

int result = string.Compare(text1, text2);

Console.Write(result); // I get 0 which means equal which is correct.

但如果我這樣做:

string text1 = "Helo";
string text2 = "Hello";

int result = string.Compare(text1, text2);

Console.Write(result); // I get 1. Shouldn't i be getting -1? Doing the opposite meaning that i have text1 = "Hello" and text 2 = "Helo" produces -1 when it should produce 1 correct?

為什么會發生這種情況,或者我錯過了(弄亂)某些東西?

它比較了每個字符的順序:H = H,E = E,L = L,O> L然后停止。 所以Helo > Hello因為l在字母表中的o之前。

更多信息可以在MSDN上找到

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM