c sharp

C# String Contains

In programming languages, we may need to check a string to a list of substrings in a variety of situations. At times, we have to work with a tangle of exceptions and need to compare the message to a list of recognized error messages to see if the error is temporary or not. To check if a string has a particular substring value, or if a specified substring exists in the string, we use C# String.Contains function. Use the Contains() method on the string and pass the value of the substring as a parameter. The article uses the Contains() function to determine whether a substring occurs in a given string in the C# programming language.

What is the String Contains function in C# in Ubuntu 20.04

This function is used to find strings. The Contains() function in C# returns a value that specifies whether or not the provided substring appears in this string. It determines whether one substring is found in another. It also has a case-sensitive ordinal mechanism for determining the contents of strings.

Syntax of the String Contains function in C# in Ubuntu 20.04

The String.Contains function, C# has the following general syntax:

# public bool String.Contains(substring)

The C# Contains function returns true if and only if the given sequence of char values is present in this string.

Return Value: The boolean value is returned. It returns True if the substring occurs in the string or if the given substring value is empty, or else False is returned.

Exception: If the substring is null, the Contains function may throw an ArgumentNullException.

How to use the String Contains function in C# in Ubuntu 20.04

To understand the basics of using the String.Contains function. Look at how the Contains function is implemented in C#.

Example 1: Using the String Contains function in C# in Ubuntu 20.04

Here’s an example of a C# program that is utilizing the Contains() method to see if the specified value is found within the string. We have to apply the Contains function over the string.

The C# program begins after importing the system library with the keyword “using”. The class is created as “stringContainsMethod” and in the class block. We have invoked the class main function with the public static void keywords. In the main function of the class, we have declared a string variable “MyStr” and initialized it with the string literals value. Then, we have declared the variables “Mysubstr1” and “Mysubstr2” where we have stored the substring values from the specified string. The writeLine statement will print the string value which is stored in the variable “Mystr”.

We have used the Contains function with the “Mystr ” and passed substring variables “Mysubstr1 ” and “Mysubstr2” in the function as parameters. Note that “Mysubstr1 ” has a substring value “Good” which exists in the string so it will return true. On the other hand, the substring “Mysubstr2” value contains “good” which is case sensitive comparison in the Contains functio hence will return false. We have Contains function in the c# writeLine statement.

Upon execution of the above program, we got the Boolean results from the given substring in the Contains function as shown below.

Example 2: Using the String Contains function with if-else in C# in Ubuntu 20.04

We can also use the Contains function with the if-else statement. We can directly use the Contains function in the if statement and return the Boolean type. Let’s implement the program that has a Contains function used in the if-else statement.

We have included the system and namespace in the above program. In the namespace, we have created the class “IfElseStringContain ”. The class has a void main function which has string type as an argument. In the main function block, we have defined a variable “StringVal” of type string. The string type variable “StringVal ” has a stored string value which will be printed through the writeLine method. Then, we have an if-else condition in which we have used the Contains function. If the statement has a Contains function for the string variable “StringVal” the substring value of the specified string has passed as a parameter. If the substring is present in the string then the writeLine method will show the message of the substring found. Otherwise, the else statement will execute. As we have substring “Aqsa” as a parameter in the Contains function so it returns true.

The output from the Contains function with the if-else statement is shown on the console screen.

Example 3: Using the String Contains function along with the initial position in C# in Ubuntu 20.04

The example below checks whether the string is a substring of the specified string or not. It also indicates its initial position if it’s located in the string.

Initially in the program, we have created a class “Example”. In the class, we have the main function. In the main function block, we have a variable declaration. The first variable, “substr1,” is declared and set with the string value. The second variable is declared as “substr2” and stores the substring value of the “substr1” string. The bool type object is defined as “Bool” where we have invoked the Contains function for the string “substr1”. The Contains function takes the substring “substr2” as a parameter and checks whether the substring is part of the given string.

The Contain function returns the Boolean value as we have used the bool type property. Then we have nested if statements for the string. The first if statement has the variable “Bool” and in the if statement block we have defined a new variable “indexPosition”. The variable “indexPosition” contains an indexOf method which shows whether the substring exists or not in the specified string. The second if statement has the condition on “indexPosition” which returns the starting index position of the substring from the given string.

The following output we got from the above program is displayed on the terminal screen.

Example 4: Using the String Contains function ignore case in C# in Ubuntu 20.04

While utilizing Contains, we can also use the case-insensitive ordinal comparison and the ordinal comparison that determines whether a substring is present in a string. These comparisons are used to ignore the case sensitivity in the string.

We have defined a class as “IgnoreCase ” in the above program and the class main function, the variable is declared. The variable is declared as “String” in which string is stored. Then, we have created an object “b” for a bool type property. The object “b” has a Contains function in which substring values have passed. Along with the substring we have also used ordinal comparison for the string. In the ordinal comparison, the letter case is taken into consideration. Then, we have used ordinal ignore case comparison in another Contains function for the given string. The ordinal ignores the case and just ignores the letter case in the string.

The output of the ignore case upon the above program execution is shown in the following image.


Finally, we have ended up our article and demonstrated all the possible ways of using the String.Contains function. We have started with the introduction of the String.Contains function and then discussed its definition. We have also shown the syntax we commonly use in C# for the String.Contains function. The various illustrations are used to handle the Contains function in the C# program. We have also demonstrated the case sensitivity of the Contains function and how to ignore it in the program. This might help resolve challenges with the Contains function.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.