共用方式為


String.EndsWith Method (String)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Function EndsWith ( _
    value As String _
) As Boolean
public bool EndsWith(
    string value
)

Parameters

Return Value

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

Exceptions

Exception Condition
ArgumentNullException

value is nulla null reference (Nothing in Visual Basic).

Remarks

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.

Examples

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


Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      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 (>).


      outputBlock.Text &= "The following lists the items before the ends have been stripped:" & vbCrLf
      outputBlock.Text &= "-----------------------------------------------------------------" & vbCrLf

      ' print out the initial array of strings
      Dim s As String
      For Each s In strSource
         outputBlock.Text &= s & vbCrLf

      Next s
      outputBlock.Text &= vbCrLf

      outputBlock.Text &= "The following lists the items after the ends have been stripped:" & vbCrLf
      outputBlock.Text &= "----------------------------------------------------------------" & vbCrLf

      ' print out the array of strings
      For Each s In strSource
         outputBlock.Text &= StripEndTags(s) & vbCrLf
      Next s

   End Sub 'Main

   Private Shared Function StripEndTags(ByVal 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
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;
   }
}

Version Information

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

Platforms

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