[go: up one dir, main page]

0% found this document useful (0 votes)
10 views1 page

complete-reference-vb_net_13

Uploaded by

khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views1 page

complete-reference-vb_net_13

Uploaded by

khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Compare

Compare

The Compare method lets you compare two String object values to each other. This static method returns 0
for a match and 1 for no match, indicating the equality, or not, of the two values (see Table 15−1). The syntax
is as follows:

String.Compare(StrA, StrB)

Consider the following code:

Dim sText, sNewText, sTextDisplay As String


Dim num As Integer
sNewText = "hello world"
sTextDisplay = "hello world"
num = sText.Compare(sNewText, sTextDisplay)
Console.WriteLine("Result: {0}", CStr(num))

The Strings are equal and the output to the console is

Result: 0

You also do not always need to call an instance method because a lot of methods provided by the String class
are static. Here's an example:

num = String.Compare(sNewText, sTextDisplay)

In Table 15−1 the static methods are denoted with the "(s)" symbol.

This fast method can tell you whether or not you have a match of values. You can ignore the reason for the
mismatch. You can also use the CompareTo method to test for actual equality of the object's values. Table
15−2 lists the return values and their meanings for the Compare method.

Table 15−2: Compare Method's Return Codes

If the Ordinal Returned Is Then


Negative ordinal strS is less than strT
0 strS and strT are equal
Positive ordinal strS is greater than strT
A similar method in the String object is CompareOrdinal. This member compares the String object without
regard for language or culture. The following line

n = sText.CompareOrdinal(sNewText, sTextDisplay)

returns the same three result ordinals as the Compare method.

CompareTo

The CompareTo method is similar to the preceding comparison method, but instead of taking two String
objects as parameters, the method compares the String parameter to the owner of the method. Consider the
following code:

497

You might also like