本文由机器翻译。若要查看英语原文,请勾选“英语”复选框。 也可将鼠标指针移到文本上,在弹出窗口中显示英语原文。
翻译
英语

String.EndsWith 方法 (String)

 

确定此字符串实例的结尾是否与指定的字符串匹配。

命名空间:   System
程序集:  mscorlib(mscorlib.dll 中)

public bool EndsWith(
	string value
)

参数

value

要与此实例末尾的子字符串进行比较的字符串。

返回值

Type: System.Boolean

如果 true 与此实例的末尾匹配,则为 value;否则为 false

Exception Condition
ArgumentNullException

valuenull

此方法比较 value 到位于相同的长度,作为此实例的末尾的子字符串 value, ,并返回指示它们是否相等。若要相等, value 必须对此同一实例的引用或与此实例的末尾匹配。

此方法执行使用当前区域性的单词 (区分大小写和区分区域性的) 比较。

对调用方的说明:

中所述 在 .NET Framework 中使用字符串的最佳做法, ,我们建议您避免调用替换为默认值,而是调用需要使用参数来显式指定的方法的字符串比较方法。若要确定是否字符串结尾以特定的子通过使用当前区域性的字符串比较规则,请调用 EndsWith(String, StringComparison) 方法重载,值为 StringComparison.CurrentCulture 为其 comparisonType 参数。

下面的示例定义 StripEndTags 方法,它使用 EndsWith(String) 方法从一行的末尾删除 HTML 结束标记。请注意, StripEndTags 方法称为以递归方式以确保将删除在行尾的多个 HTML 结束标记。

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 (var s in strSource)
            Console.WriteLine(StripEndTags(s));
    }

    private static string StripEndTags( string item ) {

        bool found = false;

        // 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 ) {
                found = true;
                item =  item.Substring( 0, lastLocation );
            }    
        }

        if (found)
           item = StripEndTags(item);

        return item;
    }
}
// The example displays the following output:
//    The following lists the items before the ends have been stripped:
//    -----------------------------------------------------------------
//    <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>
//    
//    The following lists the items after the ends have been stripped:
//    ----------------------------------------------------------------
//    <b>This is bold text
//    <H1>This is large Text
//    <b><i><font color=green>This has multiple tags
//    <b>This has <i>embedded</i> tags.
//    This line simply ends with a greater than symbol, it should not be modified>

Universal Windows Platform
4.5 后可用
.NET Framework
1.1 后可用
Portable Class Library
受以下版本支持:portable .NET platforms
Silverlight
2.0 后可用
Windows Phone Silverlight
7.0 后可用
Windows Phone
8.1 后可用
返回页首
显示: