C# program to check if a character is in a string or not

C# program to check if a character is in a string or not:

In this C# program, we will learn how to find if a character is in a string or not. Strings are a sequence of characters. String class is used for strings in C#. We can create String objects those can hold these strings. This class provides a couple of different methods those can be used to do different operations in a string in C#.

To find if a character is in a string or not, we can iterate through the characters of the string and compare each character with the given character one by one. But that will require us to write a different method which iterates through the string characters.

Instead, we can use the Contains() method defined in String class of C#. This method returns one boolean value. It takes one character as the argument and returns True or False based on if the character is in the string or not.

In this post, I will show you how to use Contains with an example and its definition.

Definition of Contains:

Contains method is defined as below:

public bool Contains(string s)

This method is a public method and it returns one boolean value. It takes one string s as the parameter and returns True if the string s is in the string. Else, it returns False.

We can simply call this method to check if a string or character is in a given string or not.

C# program:

Now, let’s write one program that uses Contains to check if a character is in a string.

using System;

namespace HelloWorld
{
    public class Program
    {
        public static void Main(string[] args)
        {
            String givenString = "Hello World !!";
            char char1 = 'H';
            char char2 = '1';
            char char3 = '!';       

            Console.WriteLine("char1 is in givenString: " + givenString.Contains(char1));
            Console.WriteLine("char2 is in givenString: " + givenString.Contains(char2));
            Console.WriteLine("char3 is in givenString: " + givenString.Contains(char3));
        }
    }
}

In this example.

  • givenString is the original string.
  • char1, char2, and char3 are three characters to check if these are in the string givenString or not.
  • The last three lines are printing the results of Contains for each of these characters. We are passing these characters to Contains and it returns one boolean value. True if the character is in the string, False if it is not.

If you run the above program, it will print the below output:

char1 is in givenString: True
char2 is in givenString: False
char3 is in givenString: True

char2 is not in givenString. So, the second line prints False. char1 and char3 are in givenString. So the first and the third statement prints True.

Using Contains with sub-strings:

This method also works with strings. i.e. you can pass a string to Contains to check if it is a sub-string or not.

Let me change the above program to work with strings:

using System;

namespace HelloWorld
{
    public class Program
    {
        public static void Main(string[] args)
        {
            String givenString = "Hello World !!";
            String str1 = "Hello";
            String str2 = " !!";
            String str3 = "HelloWorld";       

            Console.WriteLine("str1 is in givenString: " + givenString.Contains(str1));
            Console.WriteLine("str2 is in givenString: " + givenString.Contains(str2));
            Console.WriteLine("str3 is in givenString: " + givenString.Contains(str3));
        }
    }
}

Here, I changed the characters to strings str1, str2 and str3. If you run this program, it will print the below output:

str1 is in givenString: True
str2 is in givenString: True
str3 is in givenString: False

Conclusion:

In this post, we learned how to Contains method of C# and how to use it to check if a string or a chracter is in another string. This method is useful and easy to use than looping through the characters to do the same task.

You might also like: