How to use C# string Contains

The String Contains method within the C# language serves as a valuable tool for determining the presence or absence of a specified sequence of character values within a given string. This method plays a crucial role in evaluating whether the target string includes the exact sequence of characters being searched for.

bool string.containe(string str)
Parameters:

String str - input String for search

Returns:

Boolean - Yes/No

If the str Contains in the String then it returns true

If the str does not Contains in the String it returns False

For ex: "This is a Test".Contains("is") return True

"This is a Test".Contains("yes") return False

Exceptions:

System.ArgumentNullException : If the argument is null

When the Contains method is invoked, it thoroughly examines the contents of the string, scanning for the precise sequence of characters specified as the search criteria. If the sequence is found in its entirety, the method will yield a true result, indicating that the string indeed contains the exact sequence of characters being sought after. However, if even a single character within the specified sequence is not present within the string, the method will return false, indicating the absence of the desired character sequence.

Text

Using the Contains method in C#, programmers can effectively scrutinize the contents of a string and ascertain the presence or absence of a specific character sequence. This functionality empowers developers to design robust algorithms and implement conditional logic based on the presence or absence of certain character sequences within strings, enabling intricate and precise data processing.

Full Source C#
using System; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string str = null; str = "CSharp TOP 10 BOOKS"; if (str.Contains("TOP") == true) { MessageBox.Show("The string Contains() 'TOP' "); } else { MessageBox.Show("The String does not Contains() 'TOP'"); } } } }

When you run the C# program you will get the MessageBox with message "The string Contains() 'TOP' "

Conclusion

The Contains method in CSharp provides a valuable means of evaluating the presence or absence of a specified sequence of characters within a given string, offering programmers a reliable tool for building conditional logic and designing algorithms that depend on the existence of certain character sequences within strings.