C# | EndsWith() Method
Last Updated :
31 Mar, 2023
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:
CSHARP
using System;
public class GFG {
static public void Main()
{
string str1 = "Sudo Placement++" ;
string str2 = "Sudo Placement++" ;
bool x, y;
x = str1.EndsWith( "++" );
y = str2.EndsWith( "--" );
Console.WriteLine(x.ToString());
Console.WriteLine(y.ToString());
}
}
|
CSHARP
using System;
public class GFG {
static public void Main()
{
string [] input_str = {
"
<p> GeekforGeeks Computer Science Portal </p>
",
"<h1> GeekforGeeks Sudo Placement </h1>" ,
"<h2> GeekforGeeks Placement Preparation </h2>" ,
"<h3> GeekforGeeks Contribute </h3>" ,
"<h4> GeekforGeeks Contribute " ,
"<h> GeekforGeeks Interview </h>" ,
};
foreach ( var n in input_str)
Console.WriteLine(htmlEndTags(n));
}
private static string htmlEndTags( string str)
{
bool found = false ;
if (str.Trim().EndsWith( ">" )) {
int end = str.LastIndexOf( "</" );
if (end >= 0) {
found = true ;
str = str.Substring(0, end);
}
}
if (found)
str = htmlEndTags(str);
return str;
}
}
|
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:
CSHARP
using System.Threading;
using System.Globalization;
using System;
class StringStartWith {
public static void Main( string [] args)
{
string str1 = "Geeksforgeeks" ;
string str2 = "SudoPlacement " ;
bool result_a = str1.EndsWith( "Geeks" , false ,
CultureInfo.InvariantCulture);
bool result_b = str1.EndsWith( "geeks" , false ,
CultureInfo.InvariantCulture);
bool result_c = str1.EndsWith( "GEEKS" , false ,
CultureInfo.InvariantCulture);
bool result_d = str1.EndsWith( " " , false ,
CultureInfo.InvariantCulture);
bool result_x = str2.EndsWith( "Sudo" , false ,
CultureInfo.InvariantCulture);
Console.WriteLine(result_a);
Console.WriteLine(result_b);
Console.WriteLine(result_c);
Console.WriteLine(result_d);
Console.WriteLine(result_x);
}
}
|
Output:
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:
CSHARP
using System;
class Sudo {
public static void Main( string [] args)
{
string str1 = "GeeksforGeeks" ;
string str2 = "Sudo Placement" ;
string str3 = "Geeks Article" ;
bool result_a = str1.EndsWith( "Geek" ,
StringComparison.CurrentCulture);
bool result_b = str1.EndsWith( "geek" ,
StringComparison.CurrentCulture);
bool result_tt = str2.EndsWith( "Placement" ,
StringComparison.CurrentCulture);
bool result_t = str2.EndsWith( "Sudo" ,
StringComparison.CurrentCulture);
bool result_x = str3.EndsWith( "Article" ,
StringComparison.CurrentCulture);
bool result_xx = str3.EndsWith( "Geeks" ,
StringComparison.CurrentCulture);
Console.WriteLine(result_a);
Console.WriteLine(result_b);
Console.WriteLine(result_tt);
Console.WriteLine(result_t);
Console.WriteLine(result_x);
Console.WriteLine(result_xx);
}
}
|
Output:
False
False
True
False
True
False
Reference: https://docs.microsoft.com/en-us/dotnet/api/system.string.endswith?view=netframework-4.7.2
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...