Chat Zalo Chat Messenger Phone Number Đăng nhập
String.Compare Method (System) - Microsoft Learn

String.Compare Method (System) – Microsoft Learn

public: static int Compare(System::String ^ strA, int indexA, System::String ^ strB, int indexB, int length, System::Globalization::CultureInfo ^ culture, System::Globalization::CompareOptions options); public static int Compare (string? strA, int indexA, string? strB, int indexB, int length, System.Globalization.CultureInfo? culture, System.Globalization.CompareOptions options); public static int Compare (string strA, int indexA, string strB, int indexB, int length, System.Globalization.CultureInfo culture, System.Globalization.CompareOptions options); static member Compare : string * int * string * int * int * System.Globalization.CultureInfo * System.Globalization.CompareOptions -> int Public Shared Function Compare (strA As String, indexA As Integer, strB As String, indexB As Integer, length As Integer, culture As CultureInfo, options As CompareOptions) As Integer

Parameters

returns

int32

An integer indicating the lexical relationship between the two substrings, as shown in the following table.

Less than zero value condition The substring of strA precedes the substring of strB in the sort order. Zero Substrings appear in the same position in the sort order, or the length is zero. Greater than zero The substring of strA follows the substring of strB in the sort order.

Exception examples

The following

example uses the Compare(String, Int32,

String, Int32, Int32, CultureInfo, CompareOptions) method to compare the last names of two people. It then lists them in alphabetical order.

string name1 = “Jack Smith”; string name2 = “John Doe”; Get the position of the character after the spatial character. int index1 = name1. IndexOf(” “); index1 = index1 < 0 ? 0 : ++index1; int index2 = name2. IndexOf(” “); index2 = index2 < 0 ? 0 : ++index2; int length = Math.Max(name1. Length, name2. Length); Console.WriteLine(“Sorted alphabetically by last name:”); if (String.Compare(name1, index1, name2, index2, length, new CultureInfo(“en-US”), CompareOptions.IgnoreCase) < 0) Console.WriteLine(“{0}n{1}”, name1, name2); else Console.WriteLine(“{0}n{1}”, name2, name1); The example shows the following output: // Sorted alphabetically by last name: // John Doe // Jack Smith open System open System.Globalization let name1 = “Jack Smith” let name2 = “John Doe” // Get the position of the character after the spatial character. Let index1 = let i = name1. IndexOf ” ” si i < 0 then 0 else i + 1 let index2 = let i = name2. IndexOf ” ” si i < 0 then 0 else i + 1 let length = max name1. Length name2. Length printfn “Sorted alphabetically by last name:” if String.Compare(name1, index1, name2, index2, length, CultureInfo “en-US”, CompareOptions.IgnoreCase) < 0 then printfn $”{name1}n{name2}” else printfn $”{name2}n{name1}” // The example shows the following output: // Sorted alphabetically by last name: // John Doe // Jack Smith Imports System.Globalization Module Example Public Sub Main() Dim name1 As String = “Jack Smith” Dim name2 = “John Doe” ‘ Get position of space character. Dimmed index1 As integer = name1. IndexOf(” “) index1 = CInt(IIf(index1 < 0, 0, index1 – 1)) Dim index2 As Integer = name2. IndexOf(” “) index1 = CInt(IIf(index1 < 0, 0, index1 – 1)) Dim length As Integer = Math.Max(name1. Length, name2. length) console.writelin(“Sorted alphabetically by last name:”) if string.compare(name1, index1, name2, index2, length, _ new cultureinfo(“en-us”), compareoptions.ignorecase) < 0 then console.writeline(“{0}{1}{2}”, name1, vbcrlf, name2) else console.writeline(“{0}{1}{2}”, name2, vbcrlf, name1) end if end sub end module ‘ the example shows the following output; ‘ Sorted alphabetically by last name: ‘ John Doe ‘ Jack Smith

Reviews

The substrings to be compared begin in strA in the index of position A and in strB in the index of position B. The length of the first substring is the length of strA minus indexA. The length of the second substring is the length of strB minus indexB.

The number of characters to compare is the shorter of the lengths of the two substrings and the length. The indexA, indexB, and length parameters must be non-negative.

The comparison uses the culture parameter to obtain culture-specific information, such as case rules and the alphabetical order of individual characters. For example, a particular culture might specify that certain combinations of characters be treated as a single character, that uppercase and lowercase characters be compared in a particular way, or that the sort order of a character depends on the characters that precede or follow it.

One or both strA and strB may be null. By definition, any string, including String.Empty, compares a reference greater than a null reference, and two null references are compared equally with each other.

The comparison can be further specified by using the options parameter, which consists of one or more members of the System.Globalization.CompareOptions enumeration. However, because the purpose of this method is to perform a culture-sensitive string comparison, the CompareOptions.Ordinal and CompareOptions.OrdinalIgnoreCase values have no effect.

The comparison ends when an inequality is discovered or both substrings have been compared. However, if the two strings are compared equally at the end of one string and the other string has remaining characters, the string with the remaining characters is considered larger. The return value is the result of the last comparison made.

Notes for callers

Character sets include ignorable characters. The Compare(String, Int32, String, Int32, Int32, CultureInfo, CompareOptions) method does not consider these characters when performing a linguistic or culture-sensitive comparison. To recognize ignorable characters in the comparison, provide a value of Ordinal or OrdinalIgnoreCase for the options parameter.

See also

  • Equals

Contact US