Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C# String Contains() Method

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

Photo Credit to CodeToFun

🙋 Introduction

In C# programming, the string data type comes with a variety of built-in methods for efficient string manipulation.

The Contains() method is one such method that allows you to check if a specific substring is present within a given string.

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

💡 Syntax

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

Syntax
Copied
Copy To Clipboard
public bool Contains(string value);
  • value: The substring to search for within the string.

The method returns true if the specified substring is found, and false otherwise.

📄 Example

Let's dive into an example to illustrate how the Contains() method works.

Program.cs
Copied
Copy To Clipboard
using System;

class Program {
  static void Main() {
    string mainString = "C# programming is fun!";
    string searchString = "fun";

    // Check if the mainString contains the searchString
    bool containsSubstring = mainString.Contains(searchString);

    // Output the result
    Console.WriteLine($"Does the string contain '{searchString}'? {containsSubstring}");
  }
}

đŸ’ģ Testing the Program

Output
Does the string contain 'fun'? True

🧠 How the Program Works

In this example, the Contains() method is used to check if the string "C# programming is fun!" contains the substring "fun" and then prints the result.

↩ī¸ Return Value

The Contains() method returns a bool value. It is true if the specified substring is found within the string, and false otherwise.

📚 Common Use Cases

The Contains() method is particularly useful when you need to determine whether a specific sequence of characters exists within a larger string. It's commonly used for conditional logic or filtering operations based on the presence of substrings.

📝 Notes

  • The Contains() method is case-sensitive. If you need a case-insensitive search, consider converting both the main string and the search string to lowercase (or uppercase) using the ToLower() or ToUpper() methods before calling Contains().

đŸŽĸ Optimization

The Contains() method is optimized for performance, and no specific optimization is typically required. However, if you are performing multiple searches on the same string, consider storing the result in a variable to avoid redundant computations.

🎉 Conclusion

The Contains() method in C# is a valuable tool for string manipulation, allowing you to easily check for the presence of a substring within a string. It's a versatile method that finds application in various scenarios, from simple conditional checks to more complex filtering operations.

Feel free to experiment with different strings and substrings to explore the behavior of the Contains() 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
9 months ago

If you have any doubts regarding this article (C# String Contains() 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