Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.EndsWith Method (String)

Determines whether the end of this instance matches the specified string.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public bool EndsWith(
	string value
)

Parameters

value
Type: System.String
The string to match.

Return Value

Type: System.Boolean
true if value matches the end of this instance; otherwise, false.

ExceptionCondition
ArgumentNullException

value is null.

This method compares value to the substring at the end of this instance that is the same length as value, and indicates whether they are equal. To be equal, value must be a reference to this same instance, or must match the end of this instance.

This method performs a word (case-sensitive and culture-sensitive) comparison using the current culture.

Notes to Callers

Starting in Silverlight 4, the behavior of the String.EndsWith(String) method has changed. In Silverlight 4, it performs a case-sensitive and culture-sensitive comparison using the current culture to determine whether the current instance ends with value. This corresponds to the behavior of the String.EndsWith(String) method in the full .NET Framework. In Silverlight 2 and Silverlight 3, String.EndsWith(String) performs an ordinal comparison. If the common language runtime determines that a Silverlight-based application was compiled using either Silverlight 2 or Silverlight 3, it performs an ordinal comparison; otherwise, it performs a culture-sensitive comparison.

The following code example demonstrates how you can use the EndsWith method.


using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      // 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>" };

      outputBlock.Text += "The following lists the items before the ends have been stripped:" + "\n";
      outputBlock.Text += "-----------------------------------------------------------------" + "\n";

      // print out the initial array of strings
      foreach (string s in strSource)
         outputBlock.Text += s + "\n";

      outputBlock.Text += "\n";

      outputBlock.Text += "The following lists the items after the ends have been stripped:" + "\n";
      outputBlock.Text += "----------------------------------------------------------------" + "\n";

      // print out the array of strings
      foreach (string s in strSource)
         outputBlock.Text += StripEndTags(s) + "\n";
   }

   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;
   }
}


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

Show:
© 2014 Microsoft