Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to find a number in a string in C#?
To find a number in a string in C#, you can use Regular Expressions (Regex) from the System.Text.RegularExpressions namespace. The Regex pattern \d+ matches one or more consecutive digits in a string.
Syntax
Following is the syntax for creating a Regex pattern to match numbers −
Regex regex = new Regex(@"\d+"); Match match = regex.Match(inputString);
Following is the syntax for checking if a match was found −
if (match.Success) {
string number = match.Value;
}
Using Regex to Find the First Number
The Match method finds the first occurrence of a number in the string −
using System;
using System.Text.RegularExpressions;
class Demo {
static void Main() {
Regex r = new Regex(@"\d+");
Match m = r.Match("Welcome! We are open 365 days in a year!");
if (m.Success) {
Console.Write("Number: ");
Console.WriteLine(m.Value);
}
}
}
The output of the above code is −
Number: 365
Using Regex to Find All Numbers
To find all numbers in a string, use the Matches method which returns a collection of all matches −
using System;
using System.Text.RegularExpressions;
class Demo {
static void Main() {
Regex r = new Regex(@"\d+");
MatchCollection matches = r.Matches("We have 25 apples and 30 oranges, total 55 fruits.");
Console.WriteLine("Found " + matches.Count + " numbers:");
foreach (Match match in matches) {
Console.WriteLine(match.Value);
}
}
}
The output of the above code is −
Found 3 numbers: 25 30 55
Using Different Regex Patterns
You can use different patterns to match specific types of numbers −
using System;
using System.Text.RegularExpressions;
class Demo {
static void Main() {
string text = "Price: $12.99, Discount: -5%, Temperature: 23.5°C";
// Match integers only
Regex integers = new Regex(@"\b\d+\b");
Console.WriteLine("Integers: " + string.Join(", ", integers.Matches(text)));
// Match decimal numbers
Regex decimals = new Regex(@"\d+\.\d+");
Console.WriteLine("Decimals: " + string.Join(", ", decimals.Matches(text)));
// Match negative numbers
Regex negatives = new Regex(@"-\d+");
Console.WriteLine("Negative numbers: " + string.Join(", ", negatives.Matches(text)));
}
}
The output of the above code is −
Integers: 12, 99, 5, 23, 5 Decimals: 12.99, 23.5 Negative numbers: -5
Comparison of Common Regex Patterns
| Pattern | Description | Example Match |
|---|---|---|
| \d+ | One or more digits | 123, 5, 999 |
| \d+\.\d+ | Decimal numbers | 12.99, 0.5 |
| -?\d+ | Positive or negative integers | -5, 10, -999 |
| \b\d+\b | Whole number words only | 5 (not 5 from 25) |
Conclusion
Regular Expressions provide a powerful way to find numbers in strings using patterns like \d+ for basic digits. Use Match for the first occurrence, Matches for all occurrences, and customize patterns based on whether you need integers, decimals, or negative numbers.
