This documentation is archived and is not being maintained.
String.EndsWith Method
.NET Framework 1.1
Determines whether the end of this instance matches the specified String.
[Visual Basic] Public Function EndsWith( _ ByVal value As String _ ) As Boolean [C#] public bool EndsWith( string value ); [C++] public: bool EndsWith( String* value ); [JScript] public function EndsWith( value : String ) : Boolean;
Parameters
- value
- A String.
Return Value
true if the end of this instance matches value; otherwise, false.
Exceptions
| Exception Type | Condition |
|---|---|
| ArgumentNullException | value is a null reference (Nothing in Visual Basic). |
Remarks
This method compares value with the substring at the end of the current instance that has a length of value. Length.
This method performs a word (case-sensitive and culture-sensitive) search using the current culture.
Example
The following code example demonstrates how you can use the EndsWith method.
[Visual Basic] Imports System Public Class EndsWithTest Public Shared Sub Main() Dim strSource As String() = { "<b>This is bold text</b>", _ "<H1>This is large Text</H1>", _ "<b><i><font color = green>This has multiple tags</font></i></b>", _ "<b>This has <i>embedded</i> tags.</b>", _ "This line simply ends with a greater than symbol, it should not be modified>"} ' process an input file that contains html tags. ' this sample checks for multiple tags at the end of the line, rather than simply ' removing the last one. ' note: HTML markup tags always end in a greater than symbol (>). Console.WriteLine("The following lists the items before the ends have been stripped:") Console.WriteLine("-----------------------------------------------------------------") ' print out the initial array of strings Dim s As String For Each s In strSource Console.WriteLine(s) Next s Console.WriteLine() Console.WriteLine("The following lists the items after the ends have been stripped:") Console.WriteLine("----------------------------------------------------------------") ' print out the array of strings For Each s In strSource Console.WriteLine(StripEndTags(s)) Next s End Sub 'Main Private Shared Function StripEndTags(item As String) As String ' try to find a tag at the end of the line using EndsWith If item.Trim().EndsWith(">") Then ' now search for the opening tag... Dim lastLocation As Integer = item.LastIndexOf("</") If lastLocation >= 0 Then ' remove the identified section, if it is a valid region item = item.Substring(0, lastLocation) End If End If Return Item End Function 'StripEndTags End Class 'EndsWithTest [C#] using System; public class EndsWithTest { public static void Main() { // process an input file that contains html tags. // this sample checks for multiple tags at the end of the line, rather than simply // removing the last one. // note: HTML markup tags always end in a greater than symbol (>). string [] strSource = { "<b>This is bold text</b>", "<H1>This is large Text</H1>", "<b><i><font color=green>This has multiple tags</font></i></b>", "<b>This has <i>embedded</i> tags.</b>", "This line simply ends with a greater than symbol, it should not be modified>" }; Console.WriteLine("The following lists the items before the ends have been stripped:"); Console.WriteLine("-----------------------------------------------------------------"); // print out the initial array of strings foreach ( string s in strSource ) Console.WriteLine( s ); Console.WriteLine(); Console.WriteLine("The following lists the items after the ends have been stripped:"); Console.WriteLine("----------------------------------------------------------------"); // print out the array of strings foreach ( string s in strSource ) Console.WriteLine( StripEndTags( s ) ); } private static string StripEndTags( string item ) { // try to find a tag at the end of the line using EndsWith if (item.Trim().EndsWith(">")) { // now search for the opening tag... int lastLocation = item.LastIndexOf( "</" ); // remove the identified section, if it is a valid region if ( lastLocation >= 0 ) item = item.Substring( 0, lastLocation ); } return item; } } [C++] #using <mscorlib.dll> using namespace System; using namespace System::Collections; String* StripEndTags(String* item) { // try to find a tag at the end of the line using EndsWith if (item->Trim()->EndsWith(S">")) { // now search for the opening tag... int lastLocation = item->LastIndexOf(S"</"); // remove the identified section, if it is a valid region if (lastLocation >= 0) item = item->Substring(0, lastLocation); } return item; } int main() { // process an input file that contains html tags. // this sample checks for multiple tags at the end of the line, rather than simply // removing the last one. // note: HTML markup tags always end in a greater than symbol (>). String* strSource[] = { S"<b>This is bold text</b>", S"<H1>This is large Text</H1>", S"<b><i><font color=green>This has multiple tags</font></i></b>", S"<b>This has <i>embedded</i> tags.</b>", S"This line simply ends with a greater than symbol, it should not be modified>" }; Console::WriteLine(S"The following lists the items before the ends have been stripped:"); Console::WriteLine(S"-----------------------------------------------------------------"); // print out the initial array of strings IEnumerator* myEnum1 = strSource->GetEnumerator(); while (myEnum1->MoveNext()) { String* s = __try_cast<String*>(myEnum1->Current); Console::WriteLine(s); } Console::WriteLine(); Console::WriteLine(S"The following lists the items after the ends have been stripped:"); Console::WriteLine(S"----------------------------------------------------------------"); // print out the array of strings IEnumerator* myEnum2 = strSource->GetEnumerator(); while (myEnum2->MoveNext()) { String* s = __try_cast<String*>(myEnum2->Current); Console::WriteLine(StripEndTags(s)); } } [JScript] import System; public class EndsWithTest { public static function Main() : void { // process an input file that contains html tags. // this sample checks for multiple tags at the end of the line, rather than simply // removing the last one. // note: HTML markup tags always end in a greater than symbol (>). var strSource : String [] = [ "<b>This is bold text</b>", "<H1>This is large Text</H1>", "<b><i><font color=green>This has multiple tags</font></i></b>", "<b>This has <i>embedded</i> tags.</b>", "This line simply ends with a greater than symbol, it should not be modified>"]; Console.WriteLine("The following lists the items before the ends have been stripped:"); Console.WriteLine("-----------------------------------------------------------------"); // print out the initial array of strings for( var i : int in strSource ) Console.WriteLine( strSource[i] ); Console.WriteLine(); Console.WriteLine("The following lists the items after the ends have been stripped:"); Console.WriteLine("----------------------------------------------------------------"); // print out the array of strings for( i in strSource ) Console.WriteLine( StripEndTags( strSource[i] ) ); } private static function StripEndTags( item : String ) : String { // try to find a tag at the end of the line using EndsWith if (item.Trim().EndsWith(">")) { // now search for the opening tag... var lastLocation : int = item.LastIndexOf( "</" ); // remove the identified section, if it is a valid region if ( lastLocation >= 0 ) item = item.Substring( 0, lastLocation ); } return item; } } EndsWithTest.Main();
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard
See Also
String Class | String Members | System Namespace | StartsWith | Compare
Show: