C# String Contains() Method Last Updated : 06 Mar, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In C#, the String.Contains() method is used to check whether a substring exists within a given string. It returns a Boolean value (true or false) indicating whether the substring is found. By default, the method performs a case-sensitive comparison.Example 1: Here, we are using the String.Contains() method to check if the string contains the specified substring (Case-Sensitive Comparison). C# // C# program to demonstrate the // String.Contains() Method using System; class Geeks { public static void Main() { // declaring the string String str = "GeeksforGeeks"; String s1 = "for"; String s2 = "For"; bool ans; // using String.Contains() Method // Here case-sensitive comparison ans = str.Contains(s1); Console.WriteLine($"is '{s1}' is present in the '{str}': {ans}"); // Here case-insensitive comparison // return false because 'For' is not present in the 'GeeksforGeeks' ans = str.Contains(s2); Console.WriteLine($"is '{s2}' is present in the '{str}': {ans}"); } } Outputis 'for' is present in the 'GeeksforGeeks': True is 'For' is present in the 'GeeksforGeeks': False Syntax of String.Contains() Methodpublic bool Contains(string str)Parameter: It takes a single parameter str (string) which is to be checked. Type of this parameter is System.String. Return Type: Returns a boolean value. If a substring exists in a string or the value is the empty string (“”), then it returns True, otherwise returns False.Exception: This method can give ArgumentNullException if str is null.Note: This method performs the case-sensitive checking. The search will always begin from the first character position of the string and continue until the last character position.Example 2: Use the Contains() method to check the starting index of a substring. If the substring is found, you can also determine its starting index using String.IndexOf(). C# // C# program to demonstrate the // String.Contains() Method // along with the starting position using System; class Geeks { public static void Main() { string str = "Welcome to gfg"; string sub = "gfg"; // Check if the substring is // present in the main string bool b = str.Contains(sub); Console.WriteLine("'{0}' is in the string '{1}': {2}", sub, str, b); if (b) { int index = str.IndexOf(sub); if (index >= 0) Console.WriteLine("{0} begins at character position {1}", sub, index + 1); } } } Output'gfg' is in the string 'Welcome to gfg': True gfg begins at character position 12 Example 3: Check whether the substring is present in a string using ordinal comparison and case-insensitive ordinal comparison. C# // C# program to demonstrate the // String.Contains() Method using System; class Geeks { public static void Main() { // declaring the string String str = "GeeksforGeeks"; String sub = "For"; bool ans; // Here case-insensitive comparison // return false because 'For' is not present in the 'GeeksforGeeks' ans = str.Contains(sub); Console.WriteLine($"is '{sub}' is present in the '{str}': {ans}"); // Ordinal Ignore Case comparison ans = str.Contains(sub, StringComparison.OrdinalIgnoreCase); Console.WriteLine($"is '{sub}' is present in the '{str}': {ans}"); } } Outputis 'For' is present in the 'GeeksforGeeks': False is 'For' is present in the 'GeeksforGeeks': True Comment More infoAdvertise with us Next Article C# String Contains() Method M Mithun Kumar Follow Improve Article Tags : Misc C# CSharp-method CSharp-string Practice Tags : Misc Similar Reads SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e 7 min read TCP/IP Model The TCP/IP model is a framework that is used to model the communication in a network. It is mainly a collection of network protocols and organization of these protocols in different layers for modeling the network.It has four layers, Application, Transport, Network/Internet and Network Access.While 7 min read Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br 14 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Unified Modeling Language (UML) Diagrams Unified Modeling Language (UML) is a general-purpose modeling language. The main aim of UML is to define a standard way to visualize the way a system has been designed. It is quite similar to blueprints used in other fields of engineering. UML is not a programming language, it is rather a visual lan 14 min read Second Largest Element in an Array Given an array of positive integers arr[] of size n, the task is to find second largest distinct element in the array.Note: If the second largest element does not exist, return -1. Examples:Input: arr[] = [12, 35, 1, 10, 34, 1]Output: 34Explanation: The largest element of the array is 35 and the sec 14 min read Python Lists In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s 6 min read Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android 4 min read f-strings in Python Python offers a powerful feature called f-strings (formatted string literals) to simplify string formatting and interpolation. f-strings is introduced in Python 3.6 it provides a concise and intuitive way to embed expressions and variables directly into strings. The idea behind f-strings is to make 5 min read Python Operators In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types of Python operators. OPERATORS: These are the special symbols. Eg- + , * , /, 6 min read Like