
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Inserting Text into a String
|
51
string tail = "Name";
string test = "strVarName";
bool isFound = test.EndsWith(tail);
In both examples, the isFound Boolean variable is set to true, since each string is
found in
test.
To do a case-insensitive comparison, employ the static
string.Compare method. The
following two examples modify the previous two examples by performing a case-
insensitive comparison. The first is equivalent to a case-insensitive
StartsWith string
search:
string head = "str";
string test = "strVarName";
int isFound = string.Compare(head, 0, test, 0, head.Length, true);
The second is equivalent to a case-insensitive EndsWith string search:
string tail = "Name";
string test = "strVarName";
int isFound = string.Compare(tail, 0, test, (test.Length - tail.Length),
tail.Length, true);
Discussion
Use the BeginsWith or EndsWith instance methods to do a case-sensitive search for a
particular string at the beginning or end of a string. The equivalent case-insensitive
comparison requires the use of the static
Compare method in the String class. If the
return value of the
Compare method is zero, a match was found. Any other number
means that a match was not found.
See Also
See the “String.StartsWith Method,” “String.EndsWith Method,” and “String.Com-
pare Method” topics in the MSDN documentation. ...