String.CompareOrdinal Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Přetížení
| Name | Description |
|---|---|
| CompareOrdinal(String, Int32, String, Int32, Int32) |
Porovnává podřetětěce dvou zadaných String objektů vyhodnocením číselných hodnot odpovídajících Char objektů v každém podřetědci. |
| CompareOrdinal(String, String) |
Porovná dva zadané String objekty vyhodnocením číselných hodnot odpovídajících Char objektů v každém řetězci. |
CompareOrdinal(String, Int32, String, Int32, Int32)
public:
static int CompareOrdinal(System::String ^ strA, int indexA, System::String ^ strB, int indexB, int length);
public static int CompareOrdinal(string strA, int indexA, string strB, int indexB, int length);
static member CompareOrdinal : string * int * string * int * int -> int
Public Shared Function CompareOrdinal (strA As String, indexA As Integer, strB As String, indexB As Integer, length As Integer) As Integer
Parametry
- strA
- String
První řetězec, který se má použít v porovnání.
- indexA
- Int32
Počáteční index podřetězdce v strAsouboru .
- strB
- String
Druhý řetězec, který se má použít v porovnání.
- indexB
- Int32
Počáteční index podřetězdce v strBsouboru .
- length
- Int32
Maximální počet znaků v podřetěžcích, které chcete porovnat.
Návraty
32bitové celé číslo se znaménkem, které označuje lexikální vztah mezi dvěma srovnávacími znaménkami.
| Hodnota | Podmínka |
|---|---|
| Menší než nula | Podřetětěce je strA menší než podřetěžek v strB.
|
| Nula | Podřetětěce jsou stejné nebo length je nula.
|
| Větší než nula | Podřetětěce v strA je větší než podřetěžek v strB.
|
Výjimky
strA není null a indexA je větší než strA.Length.
nebo
strB není null a indexB je větší než strB.Length.
nebo
indexA, indexBnebo length je záporná.
Příklady
Následující příklad ukazuje, že CompareOrdinal a Compare používá různé pořadí řazení.
using System;
using System.Globalization;
class Test
{
public static void Main(String[] args)
{
String strLow = "abc";
String strCap = "ABC";
String result = "equal to ";
int x = 0;
int pos = 1;
// The Unicode codepoint for 'b' is greater than the codepoint for 'B'.
x = String.CompareOrdinal(strLow, pos, strCap, pos, 1);
if (x < 0) result = "less than";
if (x > 0) result = "greater than";
Console.WriteLine("CompareOrdinal(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, pos);
Console.WriteLine(" '{0}' is {1} '{2}'", strLow[pos], result, strCap[pos]);
// In U.S. English culture, 'b' is linguistically less than 'B'.
x = String.Compare(strLow, pos, strCap, pos, 1, false, new CultureInfo("en-US"));
if (x < 0) result = "less than";
else if (x > 0) result = "greater than";
Console.WriteLine("Compare(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, pos);
Console.WriteLine(" '{0}' is {1} '{2}'", strLow[pos], result, strCap[pos]);
}
}
open System
open System.Globalization
[<EntryPoint>]
let main _ =
let strLow = "abc"
let strCap = "ABC"
let result = "equal to "
let pos = 1
// The Unicode codepoint for 'b' is greater than the codepoint for 'B'.
let x = String.CompareOrdinal(strLow, pos, strCap, pos, 1)
let result =
if x < 0 then "less than"
elif x > 0 then "greater than"
else "equal to"
printfn $"CompareOrdinal(\"{strLow}\"[{pos}], \"{strCap}\"[{pos}]):"
printfn $" '{strLow[pos]}' is {result} '{strCap[pos]}'"
// In U.S. English culture, 'b' is linguistically less than 'B'.
let x = String.Compare(strLow, pos, strCap, pos, 1, false, new CultureInfo("en-US"))
let result =
if x < 0 then "less than"
elif x > 0 then "greater than"
else "equal to"
printfn $"Compare(\"{strLow}\"[{pos}], \"{strCap}\"[{pos}]):"
printfn $" '{strLow[pos]}' is {result} '{strCap[pos]}'"
0
Imports System.Globalization
Class Test
Public Shared Sub Main(args() As [String])
Dim strLow As [String] = "abc"
Dim strCap As [String] = "ABC"
Dim result As [String] = "equal to "
Dim x As Integer = 0
Dim pos As Integer = 1
' The Unicode codepoint for 'b' is greater than the codepoint for 'B'.
x = [String].CompareOrdinal(strLow, pos, strCap, pos, 1)
If x < 0 Then
result = "less than"
End If
If x > 0 Then
result = "greater than"
End If
' In U.S. English culture, 'b' is linguistically less than 'B'.
Console.WriteLine("CompareOrdinal(""{0}"".Chars({2}), ""{1}"".Chars({2})):", strLow, strCap, pos)
Console.WriteLine(" '{0}' is {1} '{2}'", strLow.Chars(pos), result, strCap.Chars(pos))
x = [String].Compare(strLow, pos, strCap, pos, 1, False, New CultureInfo("en-US"))
If x < 0 Then
result = "less than"
ElseIf x > 0 Then
result = "greater than"
End If
Console.WriteLine("Compare(""{0}"".Chars({2}), ""{1}"".Chars({2})):", strLow, strCap, pos)
Console.WriteLine(" '{0}' is {1} '{2}'", strLow.Chars(pos), result, strCap.Chars(pos))
End Sub
End Class
Poznámky
Parametr indexA, indexBa length parametry musí být nenegativní.
Počet porovnávaných znaků je menší než délka strA menší indexA, délka strB menší indexBa length.
Tato metoda provádí porovnání s rozlišováním velkých a malých písmen pomocí pravidel řazení řad. Další informace o slovech, řetězcích a řadových řazeních naleznete v tématu System.Globalization.CompareOptions. Chcete-li provést porovnání bez rozlišování malých a velkých písmen pomocí pravidel řazení řad, zavolejte Compare(String, Int32, String, Int32, Int32, StringComparison) metodu s argumentem nastaveným comparisonType na StringComparison.OrdinalIgnoreCase.
Protože CompareOrdinal(String, String) je statická metoda strA a strB může být null. Pokud jsou nullobě hodnoty , metoda vrátí hodnotu 0 (nula), která označuje, že strA a strB jsou rovny. Pokud je nullpouze jedna z hodnot , metoda považuje hodnotu, která není null, aby byla větší.
Viz také
Platí pro
CompareOrdinal(String, String)
public:
static int CompareOrdinal(System::String ^ strA, System::String ^ strB);
public static int CompareOrdinal(string strA, string strB);
static member CompareOrdinal : string * string -> int
Public Shared Function CompareOrdinal (strA As String, strB As String) As Integer
Parametry
- strA
- String
První řetězec určený k porovnání
- strB
- String
Druhý řetězec určený k porovnání
Návraty
Celé číslo, které označuje lexikální vztah mezi těmito dvěma srovnávacími čísly.
| Hodnota | Podmínka |
|---|---|
| Menší než nula |
strA je menší než strB.
|
| Nula |
strA a strB jsou stejné.
|
| Větší než nula |
strA je větší než strB.
|
Příklady
Následující příklad provádí a řadové porovnání dvou řetězců, které se liší pouze v případě.
// Sample for String.CompareOrdinal(String, String)
using System;
class Sample {
public static void Main() {
String str1 = "ABCD";
String str2 = "abcd";
String str;
int result;
Console.WriteLine();
Console.WriteLine("Compare the numeric values of the corresponding Char objects in each string.");
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
result = String.CompareOrdinal(str1, str2);
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
Console.Write("String '{0}' is ", str1);
Console.Write("{0} ", str);
Console.WriteLine("String '{0}'.", str2);
}
}
/*
This example produces the following results:
Compare the numeric values of the corresponding Char objects in each string.
str1 = 'ABCD', str2 = 'abcd'
String 'ABCD' is less than String 'abcd'.
*/
// Sample for String.CompareOrdinal(String, String)
open System
let str1 = "ABCD"
let str2 = "abcd"
printfn "\nCompare the numeric values of the corresponding Char objects in each string."
printfn $"str1 = '{str1}', str2 = '{str2}'"
let result = String.CompareOrdinal(str1, str2)
let str = if result < 0 then "less than" elif result > 0 then "greater than" else "equal to"
printf $"String '{str1}' is "
printf $"{str} "
printfn $"String '{str2}'."
(*
This example produces the following results:
Compare the numeric values of the corresponding Char objects in each string.
str1 = 'ABCD', str2 = 'abcd'
String 'ABCD' is less than String 'abcd'.
*)
' Sample for String.CompareOrdinal(String, String)
Class Sample
Public Shared Sub Main()
Dim str1 As [String] = "ABCD"
Dim str2 As [String] = "abcd"
Dim str As [String]
Dim result As Integer
Console.WriteLine()
Console.WriteLine("Compare the numeric values of the corresponding Char objects in each string.")
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2)
result = [String].CompareOrdinal(str1, str2)
str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to"))
Console.Write("String '{0}' is ", str1)
Console.Write("{0} ", str)
Console.WriteLine("String '{0}'.", str2)
End Sub
End Class
'
'This example produces the following results:
'
'Compare the numeric values of the corresponding Char objects in each string.
'str1 = 'ABCD', str2 = 'abcd'
'String 'ABCD' is less than String 'abcd'.
'
Poznámky
Tato metoda provádí porovnání s rozlišováním velkých a malých písmen pomocí pravidel řazení řad. Další informace o slovech, řetězcích a řadových řazeních naleznete v tématu System.Globalization.CompareOptions. Chcete-li provést porovnání bez rozlišování malých a velkých písmen pomocí pravidel řazení řad, zavolejte Compare(String, String, StringComparison) metodu s argumentem nastaveným comparisonType na StringComparison.OrdinalIgnoreCase.
Protože CompareOrdinal(String, String) je statická metoda strA a strB může být null. Pokud jsou nullobě hodnoty , metoda vrátí hodnotu 0 (nula), která označuje, že strA a strB jsou rovny. Pokud je nullpouze jedna z hodnot , metoda považuje hodnotu, která není null, aby byla větší.