The Java String contains function is a method that is used to check whether a particular sequence of characters is present in a given string or not. It returns a boolean value of true if the specified sequence of characters is found in the string, and false otherwise. The method takes a single argument, which is the sequence of characters to be searched for in the string. It is case-sensitive, meaning that it will only match the exact sequence of characters provided. The contains function is commonly used in string manipulation and searching operations in Java programming. Keep reading below to learn how to Java String contains in C#.

Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!

Buy Now On Amazon

Java String contains in C# With Example Code

Java String contains method is used to check whether a particular sequence of characters is present in a given string or not. Similarly, in C#, we have the Contains method to check if a string contains a specific substring or not.

To use the Contains method in C#, we need to first create a string and then call the Contains method on it. The Contains method returns a boolean value indicating whether the substring is present in the string or not.

Here’s an example code snippet to demonstrate the usage of the Contains method in C#:


string str = "Hello World";
bool contains = str.Contains("World");
Console.WriteLine(contains); // Output: True

In the above code, we have created a string “Hello World” and then checked if it contains the substring “World” using the Contains method. The output of the program is True, indicating that the substring is present in the string.

We can also perform a case-insensitive search using the Contains method by passing a StringComparison parameter to it. Here’s an example code snippet to demonstrate this:


string str = "Hello World";
bool contains = str.Contains("world", StringComparison.OrdinalIgnoreCase);
Console.WriteLine(contains); // Output: True

In the above code, we have passed the StringComparison.OrdinalIgnoreCase parameter to the Contains method to perform a case-insensitive search. The output of the program is True, indicating that the substring “world” is present in the string “Hello World” even though the case of the letters is different.

In conclusion, the Contains method in C# is a useful method to check if a string contains a specific substring or not. It can be used for both case-sensitive and case-insensitive searches.

Equivalent of Java String contains in C#

In conclusion, the equivalent function of Java’s String contains() method in C# is the String.Contains() method. This method is used to check whether a specified substring is present within a given string or not. It returns a boolean value indicating whether the substring is found or not. The String.Contains() method is a simple and efficient way to search for a substring in a string in C#. It is a useful tool for developers who are working with string manipulation in C# and need to check for the presence of a specific substring. By using this method, developers can easily and quickly determine whether a string contains a particular substring or not. Overall, the String.Contains() method is a valuable addition to the C# language and a useful tool for developers working with strings.

Contact Us