String.EndsWith 方法

定義

判斷這個字串執行個體的結尾是否符合指定的字串。

多載

EndsWith(String, Boolean, CultureInfo)

判斷當使用指定之文化特性進行比較時,這個字串執行個體的結尾是否符合指定的字串。

EndsWith(String, StringComparison)

判斷當使用指定的比較選項進行比較時,這個字串執行個體的結尾是否符合指定的字串。

EndsWith(Char)

判斷這個字串執行個體的結尾是否符合所指定字元。

EndsWith(String)

判斷這個字串執行個體的結尾是否符合指定的字串。

EndsWith(String, Boolean, CultureInfo)

來源:
String.Comparison.cs
來源:
String.Comparison.cs
來源:
String.Comparison.cs

判斷當使用指定之文化特性進行比較時,這個字串執行個體的結尾是否符合指定的字串。

C#
public bool EndsWith (string value, bool ignoreCase, System.Globalization.CultureInfo? culture);
C#
public bool EndsWith (string value, bool ignoreCase, System.Globalization.CultureInfo culture);

參數

value
String

要在這個執行個體結束時,與子字串比較的字串。

ignoreCase
Boolean

true 表示在比較時忽略大小寫,否則為 false

culture
CultureInfo

判斷如何比較此執行個體和 value 的文化特性資訊。 如果 culturenull,則會使用目前的文化特性。

傳回

如果 true 參數符合這個字串的結尾,則為 value,否則為 false

例外狀況

valuenull

範例

下列範例會判斷字串是否發生在另一個字串的結尾。 方法 EndsWith 會使用區分大小寫、不區分大小寫,以及影響搜尋結果的不同文化特性來呼叫數次。

C#
// This code example demonstrates the 
// System.String.EndsWith(String, ..., CultureInfo) method.

using System;
using System.Threading;
using System.Globalization;

class Sample 
{
    public static void Main() 
    {
        string msg1 = "Search for the target string \"{0}\" in the string \"{1}\".\n";
        string msg2 = "Using the {0} - \"{1}\" culture:";
        string msg3 = "  The string to search ends with the target string: {0}";
        bool result = false;
        CultureInfo ci;

        // Define a target string to search for.
        // U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE
        string capitalARing = "\u00c5";

        // Define a string to search. 
        // The result of combining the characters LATIN SMALL LETTER A and COMBINING 
        // RING ABOVE (U+0061, U+030a) is linguistically equivalent to the character 
        // LATIN SMALL LETTER A WITH RING ABOVE (U+00e5).
        string xyzARing = "xyz" + "\u0061\u030a";

        // Display the string to search for and the string to search.
        Console.WriteLine(msg1, capitalARing, xyzARing);

        // Search using English-United States culture.
        ci = new CultureInfo("en-US");
        Console.WriteLine(msg2, ci.DisplayName, ci.Name);

        Console.WriteLine("Case sensitive:");
        result = xyzARing.EndsWith(capitalARing, false, ci);
        Console.WriteLine(msg3, result);

        Console.WriteLine("Case insensitive:");
        result = xyzARing.EndsWith(capitalARing, true, ci);
        Console.WriteLine(msg3, result);
        Console.WriteLine();

        // Search using Swedish-Sweden culture.
        ci = new CultureInfo("sv-SE");
        Console.WriteLine(msg2, ci.DisplayName, ci.Name);

        Console.WriteLine("Case sensitive:");
        result = xyzARing.EndsWith(capitalARing, false, ci);
        Console.WriteLine(msg3, result);

        Console.WriteLine("Case insensitive:");
        result = xyzARing.EndsWith(capitalARing, true, ci);
        Console.WriteLine(msg3, result);
    }
}

/*
This code example produces the following results (for en-us culture):

Search for the target string "Å" in the string "xyza°".

Using the English (United States) - "en-US" culture:
Case sensitive:
  The string to search ends with the target string: False
Case insensitive:
  The string to search ends with the target string: True

Using the Swedish (Sweden) - "sv-SE" culture:
Case sensitive:
  The string to search ends with the target string: False
Case insensitive:
  The string to search ends with the target string: False

*/

備註

這個方法會將 value 參數與這個字串結尾的子字串進行比較,且長度與 value相同,並傳回值,指出它們是否相等。 若要相等, value 必須是這個相同實例的參考,或符合這個字串的結尾。

這個方法會使用指定的大小寫和文化特性來執行 (區分文化特性的字組) 比較。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

EndsWith(String, StringComparison)

來源:
String.Comparison.cs
來源:
String.Comparison.cs
來源:
String.Comparison.cs

判斷當使用指定的比較選項進行比較時,這個字串執行個體的結尾是否符合指定的字串。

C#
public bool EndsWith (string value, StringComparison comparisonType);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public bool EndsWith (string value, StringComparison comparisonType);

參數

value
String

要在這個執行個體結束時,與子字串比較的字串。

comparisonType
StringComparison

列舉值之一,指定這個字串和 value 的比較方式。

傳回

如果 true 參數符合這個字串的結尾,則為 value,否則為 false

屬性

例外狀況

valuenull

comparisonType 不是 StringComparison 值。

範例

下列範例會判斷字串是否以特定子字串結尾。 結果會受到文化特性選擇的影響、是否忽略大小寫,以及是否執行序數比較。

C#
// This example demonstrates the 
// System.String.EndsWith(String, StringComparison) method.

using System;
using System.Threading;

class Sample 
{
    public static void Main() 
    {
        string intro = "Determine whether a string ends with another string, " +
                   "using\n  different values of StringComparison.";

        StringComparison[] scValues = {
            StringComparison.CurrentCulture,
            StringComparison.CurrentCultureIgnoreCase,
            StringComparison.InvariantCulture,
            StringComparison.InvariantCultureIgnoreCase,
            StringComparison.Ordinal,
            StringComparison.OrdinalIgnoreCase };

        Console.WriteLine(intro);

        // Display the current culture because the culture-specific comparisons
        // can produce different results with different cultures.
        Console.WriteLine("The current culture is {0}.\n", 
                       Thread.CurrentThread.CurrentCulture.Name);
        
        // Determine whether three versions of the letter I are equal to each other. 
        foreach (StringComparison sc in scValues)
        {
            Console.WriteLine("StringComparison.{0}:", sc);
            Test("abcXYZ", "XYZ", sc);
            Test("abcXYZ", "xyz", sc);
            Console.WriteLine();
        }
    }

    protected static void Test(string x, string y, StringComparison comparison)
    {
        string resultFmt = "\"{0}\" {1} with \"{2}\".";
        string result = "does not end";

        if (x.EndsWith(y, comparison))
            result = "ends";
        Console.WriteLine(resultFmt, x, result, y);
    }
}

/*
This code example produces the following results:

Determine whether a string ends with another string, using
  different values of StringComparison.
The current culture is en-US.

StringComparison.CurrentCulture:
"abcXYZ" ends with "XYZ".
"abcXYZ" does not end with "xyz".

StringComparison.CurrentCultureIgnoreCase:
"abcXYZ" ends with "XYZ".
"abcXYZ" ends with "xyz".

StringComparison.InvariantCulture:
"abcXYZ" ends with "XYZ".
"abcXYZ" does not end with "xyz".

StringComparison.InvariantCultureIgnoreCase:
"abcXYZ" ends with "XYZ".
"abcXYZ" ends with "xyz".

StringComparison.Ordinal:
"abcXYZ" ends with "XYZ".
"abcXYZ" does not end with "xyz".

StringComparison.OrdinalIgnoreCase:
"abcXYZ" ends with "XYZ".
"abcXYZ" ends with "xyz".

*/

備註

方法會將 EndsWithvalue 參數與這個字串結尾的子字串進行比較,並傳回值,指出它們是否相等。 若要相等, value 必須是這個相同字串的參考,必須是空字串 (“”) ,或必須符合此字串的結尾。 方法所 EndsWith 執行的比較類型取決於 參數的值 comparisonType

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

EndsWith(Char)

來源:
String.Comparison.cs
來源:
String.Comparison.cs
來源:
String.Comparison.cs

判斷這個字串執行個體的結尾是否符合所指定字元。

C#
public bool EndsWith (char value);

參數

value
Char

在此執行個體結束時,與字元比較的字元。

傳回

如果 true 符合這個執行個體的結尾,則為 value,否則為 false

備註

這個方法會執行區分大小寫且不區分文化特性的序數 (比較) 。

適用於

.NET 9 和其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

EndsWith(String)

來源:
String.Comparison.cs
來源:
String.Comparison.cs
來源:
String.Comparison.cs

判斷這個字串執行個體的結尾是否符合指定的字串。

C#
public bool EndsWith (string value);

參數

value
String

要在這個執行個體結束時,與子字串比較的字串。

傳回

如果 true 符合這個執行個體的結尾,則為 value,否則為 false

例外狀況

valuenull

範例

下列範例指出陣列中的每個字串是否以句點結尾 (“。”) 。

C#
using System;

public class Example
{
   public static void Main()
   {
      String[] strings = { "This is a string.", "Hello!", "Nothing.", 
                           "Yes.", "randomize" };
      foreach (var value in strings) {
         bool endsInPeriod = value.EndsWith(".");
         Console.WriteLine("'{0}' ends in a period: {1}", 
                           value, endsInPeriod);
      }                            
   }
}
// The example displays the following output:
//       'This is a string.' ends in a period: True
//       'Hello!' ends in a period: False
//       'Nothing.' ends in a period: True
//       'Yes.' ends in a period: True
//       'randomize' ends in a period: False

下列範例會 StripEndTags 定義方法,該方法會使用 EndsWith(String) 方法從行尾移除 HTML 結束標記。 請注意,此方法 StripEndTags 會以遞歸方式呼叫,以確保移除行尾的多個 HTML 結束標記。

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 (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>

備註

這個方法會 value 與這個實例結尾的子字串比較,其長度與 value相同,並傳回是否相等的指示。 若要相等, value 必須是這個相同實例的參考,或符合這個實例的結尾。

這個方法會使用目前文化特性來執行單字 (區分大小寫和區分文化特性) 比較。

給呼叫者的注意事項

使用字串的最佳做法中所述,建議您避免呼叫取代預設值的字元串比較方法,而是呼叫需要明確指定參數的方法。 若要使用目前文化特性的字串比較規則,判斷字串是否以特定子字串結尾,請藉由呼叫 EndsWith(String, StringComparison) 方法多載,併為其comparisonType參數的值CurrentCulture明確發出訊號。 如果您不需要語言感知比較,請考慮使用 Ordinal

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0