In C#, EndsWith() is a string method. This method is used to check whether the ending of the current string instance matches with a specified string or not. If it matches, then it returns the string otherwise false. Using “foreach” loop, it is possible to check many strings. This method can be overloaded by passing different type and number of arguments to it.
- String.EndsWith(String) Method
- String.EndsWith(String, Boolean, CultureInfo) Method
- String.EndsWith(String, StringComparison) Method
String.EndsWith(String) Method
This method is used to check whether the ending of the string object matches with a particular string or not. If it matches then it returns the string otherwise return false.
Syntax:
public bool EndsWith(string input_string)
Parameter:
input_string: It is the required string which is to be compared and type of this parameter is System.String.
Return Type: This function returns the Boolean value i.e. true if it found a match, else it will return false. Return type is System.Boolean.
Below are the programs to demonstrates the use of String.EndsWith(String) method:
- Program 1:
// C# program to illustrate the// String.EndsWith(String) MethodusingSystem;publicclassGFG {// Main MethodstaticpublicvoidMain(){// Input two stringstringstr1 ="Sudo Placement++";stringstr2 ="Sudo Placement++";boolx, y;// Implement EndsWith() methodx = str1.EndsWith("++");y = str2.EndsWith("--");// Return match string "True"Console.WriteLine(x.ToString());// Return no match string "False"Console.WriteLine(y.ToString());}}chevron_rightfilter_noneOutput:True False
- Program 2:
// C# program to illustrate the// String.EndsWith (String) MethodusingSystem;publicclassGFG {// Main methodstaticpublicvoidMain(){// Html ending tag (</) will be remove// then print the resultstring[] input_str = {"<p> GeekforGeeks Computer Science Portal </p>","<h1> GeekforGeeks Sudo Placement </h1>","<h2> GeekforGeeks Placement Preparation </h2>","<h3> GeekforGeeks Conrtibute </h3>","<h4> GeekforGeeks Conrtibute ","<h> GeekforGeeks Interview </h>",};// Display result after implementation EndsWith()// method in strings ending tag to be removed.foreach(varnininput_str)Console.WriteLine(htmlEndTags(n));}privatestaticstringhtmlEndTags(stringstr){// set found falseboolfound =false;// To check ending tag to be found or notif(str.Trim().EndsWith(">")) {// To search opening tagintend = str.LastIndexOf("</");// if got ending tag then removeif(end >= 0) {// found set become Truefound =true;// update stringstr = str.Substring(0, end);}}// if found to be true then// return after removing stringif(found)str = htmlEndTags(str);returnstr;}}chevron_rightfilter_noneOutput:<p> GeekforGeeks Computer Science Portal <h1> GeekforGeeks Sudo Placement <h2> GeekforGeeks Placement Preparation <h3> GeekforGeeks Conrtibute <h4> GeekforGeeks Conrtibute <h> GeekforGeeks Interview
Note:
- If the input_string is Null then this method will give ArgumentNullException.
- This method also performs a case-sensitive and culture-sensitive comparison by using the current culture.
String.EndsWith(String, Boolean, CultureInfo) Method
This method is used to check whether the ending of current string instance matches the specified string when it compared using the specified culture. If match found, then return the string otherwise return false.
Syntax:
public bool EndsWith(string str,
bool case,
CultureInfo cul)
Parameters:
str: It is the string which is to be compared and the type of this parameter is System.String.
case: It will set true to ignore case during the comparison, otherwise false and the type of this parameter is System.Boolean.
cul: It is the Cultural information which checks how current string and str are compared. If culture is null, the current culture is used and the type of this parameter is System.Globalization.CultureInfo.
Return Value: This function returns the value of type System.Boolean that evaluates true if the str matches with the ending of current string else false.
Exception: If the value of str is null then this method will give ArgumentNullException.
Example:
// 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 Method(String, StringComparison)
This method is used to check whether the ending of current string instance matches the specified string or not when compared using the specified comparison option. If match found, then it returns the string otherwise false.
Syntax:
bool EndsWith(String str, StringComparison cType)
Parameters:
str: It is the required string which is to be compared and type of this parameter is System.String.
cType: It is one of the enumeration values that determine how current string and str are compared. Type of this parameter is System.StringComparison.
Return Value :This function returns the Boolean value i.e. true if it found a match, else it will return false. Return type is System.Boolean.
Exceptions:
- If the value of str is null then this method will give ArgumentNullException.
- If the value of cType is not a StringComparison value then this method will give ArgumentException.
Example:
// 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
Reference: https://docs.microsoft.com/en-us/dotnet/api/system.string.endswith?view=netframework-4.7.2
Recommended Posts:
- Difference between Method Overriding and Method Hiding in C#
- C# | Uri.MakeRelativeUri(Uri) Method
- Array.GetValue() Method in C# with Examples | Set - 1
- C# | CharEnumerator.MoveNext() Method
- C# | Math.IEEERemainder() Method
- C# | Substring() Method
- C# | IndexOfAny() Method
- C# | CharEnumerator.GetType() Method
- File.GetLastWriteTimeUtc() Method in C# with Examples
- MathF.Sin() Method in C# with Examples
- Double.CompareTo Method in C# with Examples
- C# | Graphics.DrawLine() Method | Set - 1
- UInt16.GetHashCode Method in C# with Examples
- Stack.Peek Method in C#
- C# | IsNullOrEmpty() Method
- C# | Math.Sign() Method
- Int64.CompareTo Method in C# with Examples
- C# | PadRight() Method
- How to use Array.BinarySearch() Method in C# | Set -1
- C# | Char.CompareTo() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

