Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract non-numeric text from a string

For example, I have this string that could change anytime I only want the alphabetic text from it:

Ferrari 5 10 15000 -5 0.2

So from that I want "Ferrari".

Sometimes there won't be a space between "Ferrari" and the numbers.

like image 587
user673906 Avatar asked Jan 25 '26 20:01

user673906


1 Answers

string str = "Ferrari 5 10 15000 -5 0.2";
string text = Regex.Match(str, @"[a-zA-Z\s]+").Value.Trim();

By also matching whitespace and then trimming the result, It will match "Some Car" in "Some Car 5 10 ...".

like image 189
Tim S. Avatar answered Jan 28 '26 10:01

Tim S.