📜  C#| EndsWith()方法

📅  最后修改于: 2021-05-29 17:59:01             🧑  作者: Mango

在C#中, EndsWith()是一个字符串方法。此方法用于检查当前字符串实例的结尾是否与指定的字符串匹配。如果匹配,则返回字符串,否则返回false。使用“ foreach ”循环,可以检查许多字符串。可以通过向其传递不同类型和数量的参数来重载此方法。

  • String.EndsWith(String)方法
  • String.EndsWith(String,Boolean,CultureInfo)方法
  • String.EndsWith(String,StringComparison)方法

String.EndsWith(String)方法

此方法用于检查字符串对象的结尾是否与特定字符串匹配。如果匹配,则返回字符串,否则返回false。
句法:

public bool EndsWith(string input_string)

范围:

返回类型:该函数返回布尔值,即true,如果它发现一个匹配,否则将返回false。返回类型为System.Boolean
下面的程序演示了String.EndsWith(String)方法的使用:

  • 程序1:
CSHARP
// C# program to illustrate the
// String.EndsWith(String) Method
using System;
 
public class GFG {
 
    // Main Method
    static public void Main()
    {
 
        // Input two string
        string str1 = "Sudo Placement++";
        string str2 = "Sudo Placement++";
        bool x, y;
 
        // Implement EndsWith() method
        x = str1.EndsWith("++");
        y = str2.EndsWith("--");
 
        // Return  match string "True"
        Console.WriteLine(x.ToString());
 
        // Return no match string "False"
        Console.WriteLine(y.ToString());
    }
}


CSHARP
// C# program to illustrate the
// String.EndsWith (String) Method
using System;
 
public class GFG {
 
    // Main method
    static public void Main()
    {
 
        // Html ending  tag ( GeekforGeeks Computer Science Portal 

",             "

GeekforGeeks Sudo Placement

",             "

GeekforGeeks Placement Preparation

",             "

GeekforGeeks Contribute

",             "

GeekforGeeks Contribute ",             " GeekforGeeks Interview ",         };           // Display result after implementation EndsWith()         // method in strings  ending tag  to be  removed.         foreach(var n in input_str)             Console.WriteLine(htmlEndTags(n));     }       private static string htmlEndTags(string str)     {           // set found false         bool found = false;           // To check ending tag to be found or not         if (str.Trim().EndsWith(">")) {               // To search  opening tag             int end = str.LastIndexOf("= 0) {                   // found set become True                 found = true;                   // update string                 str = str.Substring(0, end);             }         }           // if found to be true then         // return after removing string         if (found)             str = htmlEndTags(str);           return str;     } }


CSHARP
// C# program to illustrate the
// String. Endswith()h (string,
// bool, CultureInfo) Method
using System.Threading;
using System.Globalization;
using System;
 
class StringStartWith {
 
    // Main Method
    public static void Main(string[] args)
    {
 
        // Input string
        string str1 = "Geeksforgeeks";
        string str2 = "SudoPlacement ";
 
        // Implementaion of Endswith() function
        // test for original string
        bool result_a = str1.EndsWith("Geeks", false,
                       CultureInfo.InvariantCulture);
 
        // test for small letter string
        bool result_b = str1.EndsWith("geeks", false,
                        CultureInfo.InvariantCulture);
 
        // test for capital letter string
        bool result_c = str1.EndsWith("GEEKS", false,
                       CultureInfo.InvariantCulture);
 
        // test in no string parameter
        bool result_d = str1.EndsWith(" ", false,
                    CultureInfo.InvariantCulture);
 
        // test for strin2 argument .
        bool result_x = str2.EndsWith("Sudo", false,
                       CultureInfo.InvariantCulture);
 
        // Display result
        Console.WriteLine(result_a);
        Console.WriteLine(result_b);
        Console.WriteLine(result_c);
        Console.WriteLine(result_d);
        Console.WriteLine(result_x);
    }
}


CSHARP
// C# program to illustrate the
// EndsWith(String, StringComparison)
// method
using System;
 
class Sudo {
 
    // Main Method
    public static void Main(string[] args)
    {
 
        // Input two string
        string str1 = "GeeksforGeeks";
        string str2 = "Sudo Placement";
        string str3 = "Geeks Article";
 
        // Implementaion of startswith() function
        // test for original string1 value.
        bool result_a = str1.EndsWith("Geek",
            StringComparison.CurrentCulture);
 
        // test for small letter string1 value .
        bool result_b = str1.EndsWith("geek",
            StringComparison.CurrentCulture);
 
        // test for string2 value .
        bool result_tt = str2.EndsWith("Placement",
                    StringComparison.CurrentCulture);
 
        bool result_t = str2.EndsWith("Sudo",
            StringComparison.CurrentCulture);
 
        // test for string3 value .
        bool result_x = str3.EndsWith("Article",
                StringComparison.CurrentCulture);
 
        bool result_xx = str3.EndsWith("Geeks",
               StringComparison.CurrentCulture);
 
        // Display result
        Console.WriteLine(result_a);
        Console.WriteLine(result_b);
        Console.WriteLine(result_tt);
        Console.WriteLine(result_t);
        Console.WriteLine(result_x);
        Console.WriteLine(result_xx);
    }
}


输出:
True
False
  • 程式2:

夏普

// C# program to illustrate the
// String.EndsWith (String) Method
using System;
 
public class GFG {
 
    // Main method
    static public void Main()
    {
 
        // Html ending  tag ( GeekforGeeks Computer Science Portal 

",             "

GeekforGeeks Sudo Placement

",             "

GeekforGeeks Placement Preparation

",             "

GeekforGeeks Contribute

",             "

GeekforGeeks Contribute ",             " GeekforGeeks Interview ",         };           // Display result after implementation EndsWith()         // method in strings  ending tag  to be  removed.         foreach(var n in input_str)             Console.WriteLine(htmlEndTags(n));     }       private static string htmlEndTags(string str)     {           // set found false         bool found = false;           // To check ending tag to be found or not         if (str.Trim().EndsWith(">")) {               // To search  opening tag             int end = str.LastIndexOf("= 0) {                   // found set become True                 found = true;                   // update string                 str = str.Substring(0, end);             }         }           // if found to be true then         // return after removing string         if (found)             str = htmlEndTags(str);           return str;     } }

笔记:

  • 如果input_string为Null,则此方法将提供ArgumentNullException
  • 此方法还通过使用当前区域性执行区分大小写区域性区分的比较。

String.EndsWith(String,Boolean,CultureInfo)方法

此方法用于检查当前字符串实例的结尾是否在与指定区域性进行比较时与指定字符串匹配。如果找到匹配项,则返回字符串,否则返回false。
句法:

public bool EndsWith(string str,
                   bool case,
                   CultureInfo cul)

参数:

返回值:该函数返回System.Boolean类型的值,如果str与当前字符串的结尾匹配,则返回true ,否则返回false
异常:如果str的值为null,则此方法将提供ArgumentNullException
例子:

夏普

// C# program to illustrate the
// String. Endswith()h (string,
// bool, CultureInfo) Method
using System.Threading;
using System.Globalization;
using System;
 
class StringStartWith {
 
    // Main Method
    public static void Main(string[] args)
    {
 
        // Input string
        string str1 = "Geeksforgeeks";
        string str2 = "SudoPlacement ";
 
        // Implementaion of Endswith() function
        // test for original string
        bool result_a = str1.EndsWith("Geeks", false,
                       CultureInfo.InvariantCulture);
 
        // test for small letter string
        bool result_b = str1.EndsWith("geeks", false,
                        CultureInfo.InvariantCulture);
 
        // test for capital letter string
        bool result_c = str1.EndsWith("GEEKS", false,
                       CultureInfo.InvariantCulture);
 
        // test in no string parameter
        bool result_d = str1.EndsWith(" ", false,
                    CultureInfo.InvariantCulture);
 
        // test for strin2 argument .
        bool result_x = str2.EndsWith("Sudo", false,
                       CultureInfo.InvariantCulture);
 
        // Display result
        Console.WriteLine(result_a);
        Console.WriteLine(result_b);
        Console.WriteLine(result_c);
        Console.WriteLine(result_d);
        Console.WriteLine(result_x);
    }
}
输出:
False
True
False
False
False

String.EndsWith方法(String,StringComparison)

使用指定的比较选项进行比较时,此方法用于检查当前字符串实例的结尾是否与指定的字符串匹配。如果找到匹配项,则返回字符串,否则返回false。
句法:

bool EndsWith(String str, StringComparison cType)

参数:

返回值:该函数返回布尔值,即true,如果它发现一个匹配,否则将返回false。返回类型为System.Boolean
例外情况:

  • 如果str的值为null,则此方法将提供ArgumentNullException
  • 如果cType的值不是StringComparison值,则此方法将提供ArgumentException

例子:

夏普

// C# program to illustrate the
// EndsWith(String, StringComparison)
// method
using System;
 
class Sudo {
 
    // Main Method
    public static void Main(string[] args)
    {
 
        // Input two string
        string str1 = "GeeksforGeeks";
        string str2 = "Sudo Placement";
        string str3 = "Geeks Article";
 
        // Implementaion of startswith() function
        // test for original string1 value.
        bool result_a = str1.EndsWith("Geek",
            StringComparison.CurrentCulture);
 
        // test for small letter string1 value .
        bool result_b = str1.EndsWith("geek",
            StringComparison.CurrentCulture);
 
        // test for string2 value .
        bool result_tt = str2.EndsWith("Placement",
                    StringComparison.CurrentCulture);
 
        bool result_t = str2.EndsWith("Sudo",
            StringComparison.CurrentCulture);
 
        // test for string3 value .
        bool result_x = str3.EndsWith("Article",
                StringComparison.CurrentCulture);
 
        bool result_xx = str3.EndsWith("Geeks",
               StringComparison.CurrentCulture);
 
        // Display result
        Console.WriteLine(result_a);
        Console.WriteLine(result_b);
        Console.WriteLine(result_tt);
        Console.WriteLine(result_t);
        Console.WriteLine(result_x);
        Console.WriteLine(result_xx);
    }
}
输出:
False
False
True
False
True
False

参考: https : //docs.microsoft.com/zh-cn/dotnet/api/system。字符串.endswith?view = netframework-4.7.2