string
sThe next two examples demonstrate various methods for comparing string
s. To understand how one string
can be “greater than” or “less than” another, consider the process of alphabetizing a series of last names. The reader would, no doubt, place "Jones"
before "Smith"
, because the first letter of "Jones"
comes before the first letter of "Smith"
in the alphabet. The alphabet is more than just a set of 26 letters—it’s an ordered list of characters in which each letter occurs in a specific position. For example, Z is more than just a letter of the alphabet; it’s specifically the twenty-sixth letter of the alphabet. Computers can order characters alphabetically because they’re represented internally as numeric codes and those codes are ordered according to the alphabet so, for example, 'a'
is less than 'b'
—see Appendix C.
string
s with Equals
, CompareTo
and the Equality Operator (==
)Class string
provides several ways to compare string
s. Figure 16.3 demonstrates methods Equals
and CompareTo
and the equality operator (==
).
The condition in line 21 uses string
method Equals
to compare string1
and literal string
"hello"
(the argument) to determine whether they’re equal. Method Equals
(inherited from object
and overridden in string
) tests two string
s for equality (i.e., checks whether the string
s have identical contents). The method returns true
if the objects are equal and false
otherwise. In this case, the condition returns true
, because string1
references string
literal object "hello"
. Method Equals
uses word sorting rules that do not depend on your system’s currently selected culture. Comparing "hello"
with "HELLO"
would return false
, because the lowercase letters are different from the corresponding uppercase letters.
The condition in line 31 uses class string
’s overloaded equality operator (==
) to compare string
string1
with the literal string
"hello"
for equality. In C#, the equality operator also compares the contents of two string
s. Thus, the condition in the if
statement evaluates to true
, because the values of string1
and "hello"
are equal.
Line 41 tests whether string3
and string4
are equal to illustrate that comparisons are indeed case sensitive. Here, static
method Equals
is used to compare the values of two string
s. "Happy
Birthday"
does not equal "happy
birthday"
, so the condition fails, and the message "string3
does
not
equal
string4"
is output (line 47).
Lines 51–60 use string
method CompareTo
to compare string
s. The method returns 0
if the string
s are equal, a negative value if the string
that invokes CompareTo
is less than the string
that’s passed as an argument and a positive value if the string
that invokes CompareTo
is greater than the string
that’s passed as an argument.
Notice that CompareTo
considers string3
to be greater than string4
. The only difference between these two string
s is that string3
contains two uppercase letters in positions where string4
contains lowercase letters. The method uses sorting rules that are case and culture sensitive.
Figure 16.4 tests whether a string
begins or ends with a given string
. Method StartsWith
determines whether a string
starts with the string
passed to the method as an argument. Method EndsWith
determines whether a string
ends with the string
passed to the method as an argument.
Line 9 defines an array of string
s, which contains "started"
, "starting"
, "ended"
and "ending"
. Line 14 uses method StartsWith
, which takes a string
argument. The condition in the if
statement determines whether the current element
starts with the characters "st"
. If so, the method returns true
, and the element
is displayed along with a message.
Line 25 uses method EndsWith
to determine whether the current element
ends with the characters "ed"
. If so, the method returns true
, and the element
is displayed along with a message.