- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find a substring from a string in C#?
Set the string
string s = "Tom Cruise";
Now let’s say you need to find the substring “Tom”, then use the Contains() method.
if (s.Contains("Tom") == true) { Console.WriteLine("Substring found!"); }
The following is the code to learn how to find a substring from a string −
Example
using System; public class Demo { public static void Main() { string s = "Tom Cruise"; if (s.Contains("Tom") == true) { Console.WriteLine("Substring found!"); } else { Console.WriteLine("Substring not found!"); } } }
Output
Substring found!
Advertisements