Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# How can i split a string on first number?

I have the following string
"ABCD EFG 201 E" i want to split it on the first number that it founds a digit
and return both strings Ex. "ABCD EFG" and "201 E"

i tried Regex.Split and other stuff but i don’t get it. can someone help me please?
Thanks, Best Regards.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

not using regex cos I dont really like them – story goes "you have a problem, you decide to use regex on it, you now have 2 problems"

 char[] digits = {'0','1','2','3','4','5','6','7','8','9'};
 string s = "ABCD EFG 201 E";
 var idx = s.IndexOfAny(digits);
 if (idx !=-1){
     var first = s.Substring(0,idx);
     var second = s.Substring(idx);
 }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading