
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
C# program to check if a Substring is present in a Given String
Use the contains() method in C# to check if a substring is in a given string.
Let us say the string is −
United
Within the string, you need to find the substring “Uni”. For that, use the contains method and use it like the following code snippet −
res = str1.Contains(str2);
Example
You can try to run the following code to find a substring in a string.
using System; public class Demo { public static void Main() { string str1 = "United", str2 = "Uni"; bool res; res = str1.Contains(str2); if (res) Console.Write("The substring " + str2 + " is in the string " + str1); else Console.Write("The substring " + str2 + " is not in the string " + str1); } }
Output
The substring Uni is in the string United
- Related Articles
- Java program to check if a Substring is present in a given String
- Python Program to check if a substring is present in a given string.
- Check if substring present in string in Python
- C Program to Check if a Given String is a Palindrome?
- Java Program to Check if a string contains a substring
- Golang program to check if a string contains a substring
- C program to check if a given string is Keyword or not?
- Check if a given string is sum-string in C++
- Python Program to check if a string starts with a substring using regex
- Check if the given characters is present in Golang String
- Check if a String Contains a Substring in Linux
- How to check if string or a substring of string starts with substring in Python?
- How to check if a string contains a substring in Golang?
- C++ Program to Check if a String is Numeric
- Python program to check if a given string is number Palindrome

Advertisements