Compare( )
method defined by
String
.
The String Methods
The
String
class defines a large number of methods, and many of the methods have two
or more overloaded forms. For this reason, it is neither practical nor useful to list them all.
Instead, several of the more commonly used methods will be presented, along with examples
that illustrate them.
Comparing Strings
Perhaps the most frequently used string-handling operation is the comparison of one string
to another. Because of its importance,
String
provides a wide array of comparison methods.
These are shown in Table 22-1. Be aware that string comparisons are sensitive to cultural
differences. Comparison methods that do not pass cultural information use the currently
selected cultural settings.
www.freepdf-books.com
666
P a r t I I :
E x p l o r i n g t h e C # L i b r a r y
Method
Description
public static int
Compare(string
str1
, string
str2
)
Compares the string referred to by
str1
with
str2.
Returns greater than zero if
str1
is greater than
str2,
less than zero if
str1
is less than
str2,
and
zero if
str1
and
str2
are equal.
public static int
Compare(string
str1
, string
str2
,
bool
ignoreCase
)
Compares the string referred to by
str1
with
str2.
Returns greater than zero if
str1
is greater than
str2,
less than zero if
str1
is less than
str2,
and
zero if
str1
and
str2
are equal. If
ignoreCase
is
true
, the comparison ignores case differences.
Other wise, case differences matter.
public static int
Compare(string
str1
, string
str2
,
StringComparison
how
)
Compares the string referred to by
str1
with
str2.
Returns greater than zero if
str1
is greater than
str2,
less than zero if
str1
is less than
str2,
and
zero if
str1
and
str2
are equal. How the comparison
is per formed is specified by
how.
public static int
Compare(string
str1
, string
str2
,
bool
ignoreCase
,
CultureInfo
ci
)
Compares the string referred to by
str1
with
str2
using the cultural information passed in
ci.
Returns
greater than zero if
str1
is greater than
str2,
less
than zero if
str1
is less than
str2,
and zero if
str1
and
str2
are equal. If
ignoreCase
is
true
, the
comparison ignores case differences. Other wise,
case differences matter. The
CultureInfo
class is
defined in the
System.Globalization
namespace.
public static int
Compare(string
str1
, int
star t1
,
string
str2
, int
star t2
,
int
count
)
Compares por tions of the strings referred to by
str1
and
str2.
The comparison begins at
str1
[
star t1
] and
str2
[
star t2
] and runs for
count
characters. Returns
greater than zero if
str1
is greater than
str2,
less
than zero if
str1
is less than
str2,
and zero if
str1
and
str2
are equal.
public static int
Compare(string
str1
, int
star t1
,
string
str2
, int
star t2
,
int
count
,
bool
ignoreCase
)
Compares portions of the strings referred to by
str1
and
str2.
The comparison begins at
str1
[
start1
] and
str2
[
start2
] and runs for
count
characters. Returns
greater than zero if
str1
is greater than
str2,
less
than zero if
str1
is less than
str2,
and zero if
str1
and
str2
are equal. If
ignoreCase
is
true
, the comparison
ignores case differences. Otherwise, case differences
matter.
public static int
Compare(string
str1
, int
star t1
,
string
str2
, int
star t2
,
int
count
,
StringComparison
how
)
Compares por tions of the strings referred to by
str1
and
str2.
The comparison begins at
str1
[
star t1
]
and
str2
[
star t2
] and runs for
count
characters.
Returns greater than zero if
str1
is greater than
str2,
less than zero if
str1
is less than
str2,
and
zero if
str1
and
str2
are equal. How the comparison
is per formed is specified by
how.
T
ABLE
22-1
The
String
Comparison Methods
www.freepdf-books.com
PART II
C h a p t e r 2 2 :
S t r i n g s a n d F o r m a t t i n g
667
Of the comparison methods, the
Compare( )
method is the most versatile. It can
compare two strings in their entirety or in parts. It can use case-sensitive comparisons
or ignore case. In general, string comparisons use dictionary order to determine whether
one string is greater than, equal to, or less than another. You can also specify cultural
information that governs the comparison. The following program demonstrates several
versions of
Compare( )
:
// Compare strings.
using System;
class CompareDemo {
static void Main() {
string str1 = "one";
string str2 = "one";
T
ABLE
22-1
The
String
Comparison Methods
(continued)
public static int
Compare(string
str1
, int
star t1
,
string
str2
, int
star t2
,
int
count
,
bool
ignoreCase
,
CultureInfo
ci
)
Compares por tions of the strings referred to by
str1
and
str2
using the cultural information passed
in
ci.
The comparison begins at
str1
[
star t1
] and
str2
[
star t2
] and runs for
count
characters. Returns
greater than zero if
str1
is greater than
str2,
less
than zero if
str1
is less than
str2,
and zero if
str1
and
str2
are equal. If
ignoreCase
is true, the
comparison ignores case differences. Other wise,
case differences matter. The
CultureInfo
class is
defined in the
System.Globalization
namespace.
public static int
CompareOrdinal(string
str1
, string
str2
)
Compares the string referred to by
str1
with
str2
independently of culture, region, or language.
Returns greater than zero if
str1
is greater than
str2,
less than zero if
str1
is less than
str2,
and
zero if
str1
and
str2
are equal.
public static int
CompareOrdinal(string
str1
, int
star t1
,
string
str2
, int
star t2
,
int
count
)
Compares por tions of the strings referred to by
str1
and
str2
independently of culture, region, or
language. The comparison begins at
str1
[
star t1
]
and
str2
[
star t2
] and runs for
count
characters.
Returns greater than zero if
str1
is greater than
str2,
less than zero if
str1
is less than
str2,
and
zero if
str1
and
str2
are equal.
public int CompareTo(object
str
)
Compares the invoking string with
str.
Returns
greater than zero if the invoking string is greater
than
str,
less than zero if the invoking string is less
than
str,
and zero if the two are equal.
public int CompareTo(string
str
)
Compares the invoking string with
str.
Returns
greater than zero if the invoking string is greater
than
str,
less than zero if the invoking string is less
than
str,
and zero if the two are equal.
Method
Description
www.freepdf-books.com
668
P a r t I I :
E x p l o r i n g t h e C # L i b r a r y
string str3 = "ONE";
string str4 = "two";
string str5 = "one, too";
if(String.Compare(str1, str2) == 0)
Console.WriteLine(str1 + " and " + str2 +
" are equal.");
else
Console.WriteLine(str1 + " and " + str2 +
" are not equal.");
if(String.Compare(str1, str3) == 0)
Console.WriteLine(str1 + " and " + str3 +
" are equal.");
else
Console.WriteLine(str1 + " and " + str3 +
" are not equal.");
if(String.Compare(str1, str3, true) == 0)
Console.WriteLine(str1 + " and " + str3 +
" are equal ignoring case.");
else
Console.WriteLine(str1 + " and " + str3 +
" are not equal ignoring case.");
if(String.Compare(str1, str5) == 0)
Console.WriteLine(str1 + " and " + str5 +
" are equal.");
else
Console.WriteLine(str1 + " and " + str5 +
" are not equal.");
if(String.Compare(str1, 0, str5, 0, 3) == 0)
Console.WriteLine("First part of " + str1 + " and " +
str5 + " are equal.");
else
Console.WriteLine("First part of " + str1 + " and " +
str5 + " are not equal.");
int result = String.Compare(str1, str4);
if(result < 0)
Console.WriteLine(str1 + " is less than " + str4);
else if(result > 0)
Console.WriteLine(str1 + " is greater than " + str4);
else
Console.WriteLine(str1 + " equals " + str4);
}
}
www.freepdf-books.com
Do'stlaringiz bilan baham: |