C# Basic
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:
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.
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
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 callingContains()
.
đĸ 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:
Author
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
If you have any doubts regarding this article (C# String Contains() Method), please comment here. I will help you immediately.