- 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
C# Program to match all the digits in a string
To match all digits in a string, use C# Regex.
Firstly, set a string with digits −
string str = "These are my marks: 90 out of 100!";
Use the following regular expression to get digits in a string −
@"\d+"
The following is the code −
Example
using System; using System.Text.RegularExpressions; namespace Demo { class Program { private static void showMatch(string text, string expr) { Console.WriteLine("The Expression: " + expr); MatchCollection mc = Regex.Matches(text, expr); foreach (Match m in mc) { Console.WriteLine(m); } } static void Main(string[] args) { string str = "These are my marks: 90 out of 100!"; Console.WriteLine("Getting digits from a string..."); showMatch(str, @"\d+"); Console.ReadKey(); } } }
Output
Getting digits from a string... The Expression: \d+ 90 100
Advertisements