C 比較字串

2021-07-08 10:06:28 字數 4507 閱讀 4994

.net framework 提供多個方法來比較字串的值。下表列出並描述了這些值比較方法。

方法名使用string.compare

比較兩個字串的值。返回整數值。

string.compareordinal

比較兩個字串而不考慮本地區域性。返回整數值。

string.compareto

將當前字串物件與另乙個字串進行比較。返回整數值。

string.startswith

確定乙個字串是否以傳遞的字串開頭。返回布林值。

string.endswith

確定乙個字串是否以傳遞的字串結尾。返回布林值。

string.equals

確定兩個字串是否相同。返回布林值。

string.indexof

返回字元或字串的索引位置,索引位置從正在檢查的字串的開頭開始。返回整數值。

string.lastindexof

返回字元或字串的索引位置,索引位置從正在檢查的字串的結尾開始。返回整數值。

string.compare方法提供了將當前字串物件與另乙個字串或物件進行全面比較的方法。此方法識別區域性。可以使用此函式來比較兩個字串或兩個字串的子串。另外,還提供了考慮或忽略大小寫與區域性差異的過載。下表說明了此方法可能返回的三個整數值。

值型別條件負整數

stra 小於 strb。

stra 等於 strb。

正整數- 或 -

此例項大於 value。

- 或 -

value 是乙個空引用(在 visual basic 中為nothing)。

下面的示例使用compare方法來確定兩個字串的相對值。

string mystring = 

"hello world!

";

console.writeline(string.compare(mystring,

"hello world?

"));

此示例將 -1 顯示到控制台。

前乙個示例預設情況下區分區域性。如要執行不區分區域性的字串比較,請使用string.compare方法的過載,此方法過載允許通過提供 culture 引數指定要使用的區域性。若要檢視演示如何使用string.compare方法執行不區分區域性的比較的示例,請參見執行不區分區域性的字串比較。

string.compareordinal方法比較兩個字串物件而不考慮本地區域性。此方法的返回值與前乙個表中的compare方法返回的值相同。

下面的示例使用compareordinal方法來比較兩個字串的值。

string mystring = 

"hello world!

";

console.writeline(string.compareordinal(mystring,

"hello world!

"));

此示例將 

-32 顯示到控制台。

string.compareto方法將當前字串物件封裝的字串與另乙個字串或物件進行比較。此方法的返回值與前乙個表中的compare方法返回的值相同。

下面的示例使用compareto方法將 mystring 物件與 otherstring 物件進行比較。

string mystring = 

"hello world

";string otherstring =

"hello world!

"; int myint = mystring.compareto(otherstring);

console.writeline( myint );

此示例將 1 顯示到控制台。

string.compareto方法的所有過載在預設情況下都會執行區分區域性和區分大小寫的比較。未提供此方法的允許執行不區分區域性的比較的過載。為了使**清楚,建議您使用string.compare方法,為區分區域性的操作指定 cultureinfo.currentculture 或為不區分區域性的操作指定 cultureinfo.invariantculture。若要檢視演示如何使用string.compare方法執行區分區域性和不區分區域性這兩種比較的示例,請參見執行不區分區域性的字串比較。

string.equals方法可以輕易地確定兩個字串是否相同。這個區分大小寫的方法返回布林值truefalse。它可以在現有的類中使用,這一點將在下乙個示例中闡釋。下面的示例使用equals方法來確定乙個字串物件是否包含片語「hello world」。

string mystring = 

"hello world

";console.writeline(mystring.equals(

"hello world

"));

此示例將 true 顯示到控制台。

此方法也可用作靜態方法。下面的示例使用靜態方法比較兩個字串物件。

string mystring = 

"hello world

"; string yourstring =

"hello world

";console.writeline(string.equals(mystring, yourstring));

此示例將 

true 顯示到控制台。

可以使用string.startswith方法來確定乙個字串物件是否以構成另乙個字串的同一組字元開始。如果當前字串物件以傳遞的字串開始,則這個區分大小寫的方法返回true,否則返回false。下面的示例使用此方法來確定乙個字串物件是否以「hello」開始。

string mystring = 

"hello world

";console.writeline(mystring.startswith(

"hello

"));

此示例將 true 顯示到控制台。

string.endswith方法將傳遞的字串與位於當前字串物件結尾處的字元進行比較。它也返回布林值。下面的示例使用endswith方法檢查字串的結尾。

string mystring = 

"hello world

";

console.writeline(mystring.endswith(

"hello

"));

可以使用string.indexof方法來確定特定字元在字串中第一次出現的位置。這個區分大小寫的方法從字串的開頭開始計數,並使用從零開始的索引返回傳遞的字元的位置。如果無法找到該字元,則返回值 –1。

下面的示例使用indexof方法搜尋字元「l」在字串中第一次出現的位置。

string mystring = 

"hello world

";console.writeline(mystring.indexof(

'l'));

此示例將 2 顯示到控制台。

string.lastindexof方法類似於string.indexof方法,不同之處在於它返回特定字元在字串中最後出現的位置。它區分大小寫,並使用從零開始的索引。

下面的示例使用lastindexof方法來搜尋字元「l」在字串中最後出現的位置。

string mystring = 

"hello world

";

console.writeline(mystring.lastindexof(

'l'));

此示例將 9 顯示到控制台。

當與string.remove方法一起使用時,這兩種方法都非常有用。可以使用indexoflastindexof方法來檢索字元的位置,然後將該位置提供給remove方法以便移除字元或移除以該字元開頭的單詞。

C 字串比較

1,str1.equals str2 2,int result string.compare str1,str2 int result string.compare str1,str2 true 忽略大小寫比較 3 在某些語言中,可以利用 來直接比較字串,而在 c 中,只能用 來比較兩個字串是否相等...

C 字串的比較

net framework 提供多個方法來比較字串的值。下表列出並描述了這些值比較方法。方法名使用 string.compare 比較兩個字串的值。返回整數值。string.compareordinal 比較兩個字串而不考慮本地區域性。返回整數值。string.compareto 將當前字串物件與另...

C 超短字串比較

最近這幾天的工作中用到了 c 字串比較。在一次執行中需要做海量的字串相等的比較。而且字串都是 3 5位元組長度的字串,在這裡在占用了太多的 cpu資源。如何快速比較短字串,這裡也是有方法的。學習了nginx字串比較 首先思路轉化,字串比較在 cpu指令中是逐字節比較,比如有 abc 和 abd 這兩...