Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C# String EndsWith() Method

Posted in C# Tutorial
Updated on Jan 11, 2024
By Mari Selvan
👁ī¸ 59 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
C# String EndsWith() Method

Photo Credit to CodeToFun

🙋 Introduction

In C# programming, working with strings is a common and essential task.

The EndsWith() method is a member of the System.String class and is used to determine whether the end of the string instance matches a specified substring.

In this tutorial, we'll explore the usage and functionality of the EndsWith() method in C#.

💡 Syntax

The syntax for the EndsWith() method is as follows:

Syntax
Copied
Copy To Clipboard
public bool EndsWith(string value);
  • value: The substring to compare.

The method returns a bool value, indicating whether the string ends with the specified substring.

📄 Example

Let's dive into examples to illustrate how the EndsWith() method works.

Program.cs
Copied
Copy To Clipboard
using System;

class Program {
  static void Main() {
    string sampleString = "Hello, World";

    // Check if the string ends with "World"
    bool endsWithCSharp = sampleString.EndsWith("World");

    Console.WriteLine($"Example 1: {endsWithCSharp}");
    
    // case-insensitive check
    bool endsWithWorld = sampleString.EndsWith("world", StringComparison.OrdinalIgnoreCase);
    
    Console.WriteLine($"Example 2: {endsWithWorld}");
  }
}

đŸ’ģ Testing the Program

Output
Example 1: True
Example 2: True

🧠 How the Program Works

In Example 1, the EndsWith() method is used to check if the string "Hello, World" ends with the specified substring World.

In Example 2, a case-insensitive check is performed to see if the string ends with the substring world.

↩ī¸ Return Value

The EndsWith() method returns true if the string instance ends with the specified substring; otherwise, it returns false.

📚 Common Use Cases

The EndsWith() method is particularly useful when you need to perform conditional logic based on the ending characters of a string. It's commonly used in scenarios where you want to check file extensions, domain names, or specific patterns.

📝 Notes

  • The comparison is case-sensitive by default, but you can use overloads that allow you to specify whether the comparison should be case-insensitive.
  • If the specified substring is an empty string, the method returns true.

đŸŽĸ Optimization

The EndsWith() method is optimized for efficiency, and no specific optimizations are typically needed. If you're performing multiple checks with the same substring, consider storing the result in a variable to avoid redundant computations.

🎉 Conclusion

The EndsWith() method in C# provides a straightforward way to check whether a string ends with a specific substring. It's a valuable tool for string manipulation tasks and conditional logic.

Feel free to experiment with different strings and substrings to deepen your understanding of the EndsWith() method. Happy coding!

👨‍đŸ’ģ Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
2 months ago

If you have any doubts regarding this article (C# String EndsWith() Method), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy