本文整理匯總了VB.NET中System.String.Compare方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET String.Compare方法的具體用法?VB.NET String.Compare怎麽用?VB.NET String.Compare使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.String
的用法示例。
在下文中一共展示了String.Compare方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Example
Public Module Example
Public Sub Main()
' Create upper-case characters from their Unicode code units.
Dim stringUpper As String = ChrW(&H41) + ChrW(&H42) + ChrW(&H43)
' Create lower-case characters from their Unicode code units.
Dim stringLower As String = ChrW(&H61) + ChrW(&H62) + ChrW(&H63)
' Display the strings.
Console.WriteLine("Comparing '{0}' and '{1}':",
stringUpper, stringLower)
' Compare the uppercased strings; the result is true.
Console.WriteLine("The Strings are equal when capitalized? {0}",
If(String.Compare(stringUpper.ToUpper(), stringLower.ToUpper()) = 0,
"true", "false"))
' The previous method call is equivalent to this Compare method, which ignores case.
Console.WriteLine("The Strings are equal when case is ignored? {0}",
If(String.Compare(stringUpper, stringLower, true) = 0,
"true", "false"))
End Sub
End Module
輸出:
Comparing 'ABC' and 'abc': The Strings are equal when capitalized? true The Strings are equal when case is ignored? true
示例2: SamplesArrayList
' 導入命名空間
Imports System.Text
Imports System.Collections
Public Class SamplesArrayList
Public Shared Sub Main()
Dim myAL As New ArrayList()
' Creates and initializes a new ArrayList.
myAL.Add("Eric")
myAL.Add("Mark")
myAL.Add("Lance")
myAL.Add("Rob")
myAL.Add("Kris")
myAL.Add("Brad")
myAL.Add("Kit")
myAL.Add("Bradley")
myAL.Add("Keith")
myAL.Add("Susan")
' Displays the properties and values of the ArrayList.
Console.WriteLine("Count: {0}", myAL.Count)
PrintValues("Unsorted", myAL)
myAL.Sort()
PrintValues("Sorted", myAL)
Dim comp as New ReverseStringComparer
myAL.Sort(comp)
PrintValues("Reverse", myAL)
Dim names As String() = CType(myAL.ToArray(GetType(String)), String())
End Sub
Public Shared Sub PrintValues(title As String, myList As IEnumerable)
Console.Write("{0,10}: ", title)
Dim sb As New StringBuilder()
Dim s As String
For Each s In myList
sb.AppendFormat("{0}, ", s)
Next s
sb.Remove(sb.Length - 2, 2)
Console.WriteLine(sb)
End Sub
End Class
Public Class ReverseStringComparer
Implements IComparer
Function Compare(x As Object, y As Object) As Integer implements IComparer.Compare
Dim s1 As String = CStr (x)
Dim s2 As String = CStr (y)
'negate the return value to get the reverse order
Return - [String].Compare(s1, s2)
End Function 'Compare
End Class
示例3: IsFileURI
Shared Function IsFileURI(ByVal path As String) As Boolean
If String.Compare(path, 0, "file:", 0, 5, True) = 0 Then
Return True
Else
Return False
End If
End Function
示例4: IsFileURI
Shared Function IsFileURI(ByVal path As String) As Boolean
If String.Compare(path, 0, "file:", 0, 5, StringComparison.OrdinalIgnoreCase) = 0 Then
Return True
Else
Return False
End If
End Function
示例5: Example
Module Example
Public Sub Main()
Dim s1 As String = "ani" + ChrW(&h00AD) + "mal"
Dim s2 As String = "animal"
Console.WriteLine("Comparison of '{0}' and '{1}': {2}",
s1, s2, String.Compare(s1, s2))
End Sub
End Module
輸出:
Comparison of 'ani-mal' and 'animal': 0
示例6: Example
Module Example
Public Sub Main()
Dim s1 As String = "Ani" + ChrW(&h00AD) + "mal"
Dim s2 As String = "animal"
Console.WriteLine("Comparison of '{0}' and '{1}': {2}",
s1, s2, String.Compare(s1, s2, True))
End Sub
End Module
輸出:
Comparison of 'ani-mal' and 'animal': 0
示例7: Sample
' This example demonstrates the
' System.String.Compare(String, String, StringComparison) method.
Imports System.Threading
Class Sample
Public Shared Sub Main()
Dim intro As String = "Compare three versions of the letter I using different " & _
"values of StringComparison."
' Define an array of strings where each element contains a version of the
' letter I. (An array of strings is used so you can easily modify this
' code example to test additional or different combinations of strings.)
Dim threeIs(2) As String
' LATIN SMALL LETTER I (U+0069)
threeIs(0) = "i"
' LATIN SMALL LETTER DOTLESS I (U+0131)
threeIs(1) = "ı"
' LATIN CAPITAL LETTER I (U+0049)
threeIs(2) = "I"
Dim unicodeNames As String() = { _
"LATIN SMALL LETTER I (U+0069)", _
"LATIN SMALL LETTER DOTLESS I (U+0131)", _
"LATIN CAPITAL LETTER I (U+0049)" }
Dim scValues As StringComparison() = { _
StringComparison.CurrentCulture, _
StringComparison.CurrentCultureIgnoreCase, _
StringComparison.InvariantCulture, _
StringComparison.InvariantCultureIgnoreCase, _
StringComparison.Ordinal, _
StringComparison.OrdinalIgnoreCase }
'
Console.Clear()
Console.WriteLine(intro)
' Display the current culture because the culture-specific comparisons
' can produce different results with different cultures.
Console.WriteLine("The current culture is {0}." & vbCrLf, _
Thread.CurrentThread.CurrentCulture.Name)
' Determine the relative sort order of three versions of the letter I.
Dim sc As StringComparison
For Each sc In scValues
Console.WriteLine("StringComparison.{0}:", sc)
' LATIN SMALL LETTER I (U+0069) : LATIN SMALL LETTER DOTLESS I (U+0131)
Test(0, 1, sc, threeIs, unicodeNames)
' LATIN SMALL LETTER I (U+0069) : LATIN CAPITAL LETTER I (U+0049)
Test(0, 2, sc, threeIs, unicodeNames)
' LATIN SMALL LETTER DOTLESS I (U+0131) : LATIN CAPITAL LETTER I (U+0049)
Test(1, 2, sc, threeIs, unicodeNames)
Console.WriteLine()
Next sc
End Sub
Protected Shared Sub Test(ByVal x As Integer, ByVal y As Integer, _
ByVal comparison As StringComparison, _
ByVal testI() As String, ByVal testNames() As String)
Dim resultFmt As String = "{0} is {1} {2}"
Dim result As String = "equal to"
Dim cmpValue As Integer = 0
'
cmpValue = String.Compare(testI(x), testI(y), comparison)
If cmpValue < 0 Then
result = "less than"
ElseIf cmpValue > 0 Then
result = "greater than"
End If
Console.WriteLine(resultFmt, testNames(x), result, testNames(y))
End Sub
End Class
輸出:
Compare three versions of the letter I using different values of StringComparison. The current culture is en-US. StringComparison.CurrentCulture: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.CurrentCultureIgnoreCase: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.InvariantCulture: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.InvariantCultureIgnoreCase: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.Ordinal: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is greater than LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.OrdinalIgnoreCase: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)
示例8: Sample
' 導入命名空間
Imports System.Globalization
_
Class Sample
Public Shared Sub Main()
Dim str1 As [String] = "change"
Dim str2 As [String] = "dollar"
Dim relation As [String] = Nothing
relation = symbol([String].Compare(str1, str2, False, New CultureInfo("en-US")))
Console.WriteLine("For en-US: {0} {1} {2}", str1, relation, str2)
relation = symbol([String].Compare(str1, str2, False, New CultureInfo("cs-CZ")))
Console.WriteLine("For cs-CZ: {0} {1} {2}", str1, relation, str2)
End Sub
Private Shared Function symbol(r As Integer) As [String]
Dim s As [String] = "="
If r < 0 Then
s = "<"
Else
If r > 0 Then
s = ">"
End If
End If
Return s
End Function 'symbol
End Class
輸出:
For en-US: change < dollar For cs-CZ: change > dollar
示例9: Example
' 導入命名空間
Imports System.Globalization
Module Example
Public Sub Main()
Dim s1 As String = "Ani" + ChrW(&h00AD) + "mal"
Dim s2 As String = "animal"
Console.WriteLine("Comparison of '{0}' and '{1}': {2}",
s1, s2, String.Compare(s1, s2, True,
CultureInfo.InvariantCulture))
End Sub
End Module
輸出:
Comparison of 'ani-mal' and 'animal': 0
示例10: Example
' 導入命名空間
Imports System.Globalization
Public Module Example
Public Sub Main()
Dim string1 As String = "brother"
Dim string2 As String = "Brother"
Dim relation As String
Dim result As Integer
' Cultural (linguistic) comparison.
result = String.Compare(string1, string2, _
New CultureInfo("en-US"), CompareOptions.None)
If result > 0 Then
relation = "comes after"
ElseIf result = 0 Then
relation = "is the same as"
Else
relation = "comes before"
End If
Console.WriteLine("'{0}' {1} '{2}'.", string1, relation, string2)
' Cultural (linguistic) case-insensitive comparison.
result = String.Compare(string1, string2, _
New CultureInfo("en-US"), CompareOptions.IgnoreCase)
If result > 0 Then
relation = "comes after"
ElseIf result = 0 Then
relation = "is the same as"
Else
relation = "comes before"
End If
Console.WriteLine("'{0}' {1} '{2}'.", string1, relation, string2)
' Culture-insensitive ordinal comparison.
result = String.CompareOrdinal(string1, string2)
If result > 0 Then
relation = "comes after"
ElseIf result = 0 Then
relation = "is the same as"
Else
relation = "comes before"
End If
Console.WriteLine("'{0}' {1} '{2}'.", string1, relation, string2)
End Sub
End Module
輸出:
brother' comes before 'Brother'. brother' is the same as 'Brother'. brother' comes after 'Brother'.
示例11: Sample
' Sample for String.Compare(String, Int32, String, Int32, Int32)
Class Sample
Public Shared Sub Main()
' 0123456
Dim str1 As [String] = "machine"
Dim str2 As [String] = "device"
Dim str As [String]
Dim result As Integer
Console.WriteLine()
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2)
result = [String].Compare(str1, 2, str2, 0, 2)
str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to"))
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1)
Console.Write("{0} ", str)
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2)
End Sub
End Class
輸出:
str1 = 'machine', str2 = 'device' Substring 'ch' in 'machine' is less than substring 'de' in 'device'.
示例12: Sample
' Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean)
Class Sample
Public Shared Sub Main()
' 0123456
Dim str1 As [String] = "MACHINE"
Dim str2 As [String] = "machine"
Dim str As [String]
Dim result As Integer
Console.WriteLine()
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2)
Console.WriteLine("Ignore case:")
result = [String].Compare(str1, 2, str2, 2, 2, True)
str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to"))
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1)
Console.Write("{0} ", str)
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2)
Console.WriteLine()
Console.WriteLine("Honor case:")
result = [String].Compare(str1, 2, str2, 2, 2, False)
str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to"))
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1)
Console.Write("{0} ", str)
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2)
End Sub
End Class
輸出:
str1 = 'MACHINE', str2 = 'machine' Ignore case: Substring 'CH' in 'MACHINE' is equal to substring 'ch' in 'machine'. Honor case: Substring 'CH' in 'MACHINE' is greater than substring 'ch' in 'machine'.
示例13: Sample
' Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean, CultureInfo)
Imports System.Globalization
Class Sample
Public Shared Sub Main()
' 0123456
Dim str1 As [String] = "MACHINE"
Dim str2 As [String] = "machine"
Dim str As [String]
Dim result As Integer
Console.WriteLine()
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2)
Console.WriteLine("Ignore case, Turkish culture:")
result = [String].Compare(str1, 4, str2, 4, 2, True, New CultureInfo("tr-TR"))
str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to"))
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4, 2), str1)
Console.Write("{0} ", str)
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4, 2), str2)
Console.WriteLine()
Console.WriteLine("Ignore case, invariant culture:")
result = [String].Compare(str1, 4, str2, 4, 2, True, CultureInfo.InvariantCulture)
str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to"))
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4, 2), str1)
Console.Write("{0} ", str)
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4, 2), str2)
End Sub
End Class
輸出:
str1 = 'MACHINE', str2 = 'machine' Ignore case, Turkish culture: Substring 'IN' in 'MACHINE' is less than substring 'in' in 'machine'. Ignore case, invariant culture: Substring 'IN' in 'MACHINE' is equal to substring 'in' in 'machine'.
示例14: Example
' 導入命名空間
Imports System.Globalization
Module Example
Public Sub Main()
Dim name1 As String = "Jack Smith"
Dim name2 = "John Doe"
' Get position of space character.
Dim 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.WriteLIne("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
輸出:
Sorted alphabetically by last name: John Doe Jack Smith
示例15: MainClass
' 導入命名空間
Imports System
Imports System.Collections
Public Class MainClass
Shared Sub Main()
Dim s1 As String = "abcd"
Dim s2 As String = "ABCD"
Dim result As Integer
result = String.Compare(s1, s2)
Console.WriteLine( _
"compare s1: {0}, s2: {1}, result: {2}" _
& Environment.NewLine, s1, s2, result)
result = String.Compare(s1, s2, True)
Console.WriteLine("Compare insensitive. result: {0}" _
& Environment.NewLine, result)
End Sub
End Class