[SOLVED] Get first numbers from String


How to get the first numbers from a string?

Example: I have “1567438absdg345”

I only want to get “1567438” without “absdg345”, I want it to be dynamic, get the first occurrence of Alphabet index and remove everything after it.


You can use the TakeWhile extension methods to get characters from the string as long as they are digits:

string input = "1567438absdg345";

string digits = new String(input.TakeWhile(Char.IsDigit).ToArray());

Answered By – Guffa

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *