C# | EndsWith() Method

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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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());
        }
    }

    chevron_right

    
    

    Output:

    True
    False
    

  • Program 2:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to illustrate the
    // String.EndsWith (String) Method
    using System;
      
    public class GFG {
      
        // Main method
        static public void Main()
        {
      
            // Html ending  tag (</) will be remove
            // then print the result
            string[] 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(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("</");
      
                // if got ending tag then remove
                if (end >= 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;
        }
    }

    chevron_right

    
    

    Output:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

False
False
True
False
True
False

Reference: https://docs.microsoft.com/en-us/dotnet/api/system.string.endswith?view=netframework-4.7.2



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.