find and solve || findandsolve.com
Please wait.....

C# String EndsWith()

In the c# string EndsWith method compares the value parameter to the substring at the end of this string and returns a value that check whether the ending of the current string matches with a specified string or not. Matches value then it returns the string otherwise false

Syntax
public bool EndsWith(String str)  

public bool EndsWith(String, Boolean, CultureInfo)
public bool EndsWith (String, StringComparison)  

public bool EndsWith(String str)  

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

Example
using System;
public class Program {
    // Main Method
    public static void Main()
    {
     // Input two string
        string strFirst = "FindAnd Solve!";
        string strSecond = "FindAndSolve!";
        bool a, b;
        // Implement EndsWith() method
        a = strFirst.EndsWith("!");
        b = strSecond.EndsWith("+");
        // Return match string "True"
        Console.WriteLine(a.ToString());
        // Return no match string "False"
        Console.WriteLine(b.ToString());
 }
}
Output

True

False

String.EndsWith(String, Boolean, CultureInfo) Method

Determines whether the end of this string instance matches the specified string when compared using the specified culture.

Syntax
public bool EndsWith(string str, bool case,CultureInfo cul)
  • objStr: It is the string which is to be compared and the type of this parameter is string type (System.String).
  • objCase: It will set true to ignore case during the comparison, otherwise false and the type of this parameter is boolean type (System.Boolean).
  • objCul: 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.

Example

using System;
public class Program {
    // Main Method
    public static void Main()
    {
       // Input string
        string strFirst = "FomdAndSolveDotCom";
        string strSecond = "IsSolveDotCom ";
        // Implementation of Endswith() function
        // test for original string
        bool result1 = strFirst.EndsWith("Com", false,
                       CultureInfo.InvariantCulture);
        // test for small letter string
        bool result2 = strFirst.EndsWith("Demo", false,
                        CultureInfo.InvariantCulture);
        // test for capital letter string
        bool result3 = strSecond.EndsWith("Find", false,
                       CultureInfo.InvariantCulture);
        // test in no string parameter
        bool result4 = strFirst.EndsWith(" ", false,
                    CultureInfo.InvariantCulture);
        // test for strin2 argument .
        bool result5 = strSecond.EndsWith("And", false,
                       CultureInfo.InvariantCulture);
        // Display result
        Console.WriteLine(result1);
        Console.WriteLine(result2);
        Console.WriteLine(result3);
        Console.WriteLine(result4);
        Console.WriteLine(result5);
    }
 }

Output

True

False

False

False

False


public EndsWith(String, StringComparison)

Determines whether the end of this string instance matches the specified string when compared using the specified comparison option.

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: If it found a match the return true else it will return false. Return type is System.Boolean.


Example

using System;
public class Program {
    // Main Method
    public static void Main()
    {
       // Input two string
        string strFirst = "FindAndSolveDotCom";
        string strSecond = "IsSolveDotCom";
        string strThird = "Information Technology";
        // Implementation of startswith() function
        // test for original string1 value.
        bool result1 = strFirst.EndsWith("Com",
            StringComparison.CurrentCulture);
        // test for small letter string1 value .
        bool result2 = strFirst.EndsWith("Com",
            StringComparison.CurrentCulture);
        // test for string2 value .
        bool result3 = strSecond.EndsWith("Test",
                    StringComparison.CurrentCulture);
        bool result4 = strSecond.EndsWith("Sudo",
            StringComparison.CurrentCulture);
        // test for string3 value .
        bool result5 = strThird.EndsWith("Technology",
                StringComparison.CurrentCulture);
        bool result6 = strThird.EndsWith("Com",
               StringComparison.CurrentCulture);
        // Display result
        Console.WriteLine(result1);
        Console.WriteLine(result2);
        Console.WriteLine(result3);         Console.WriteLine(result4);
        Console.WriteLine(result5);         Console.WriteLine(result6);
    }
 }

Output

True

True

False

False

True

False