String.EndsWith Method (String)
Determines whether the end of this instance matches the specified string.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
- Type: System.String
The string to match.
| Exception | Condition |
|---|---|
| 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 CallersStarting 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; } }
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.