I have strings like
AS_!SD 2453iur ks@d9304-52kasd
I need to get the 2 frist numbres of the string:
for that case will be:
Alternatively you can use the ASCII encoding:
string value = "AS_!SD 2453iur ks@d9304-52kasd";
byte zero = 48; // 0
byte nine = 57; // 9
byte[] asciiBytes = Encoding.ASCII.GetBytes(value);
byte[] asciiNumbers = asciiBytes.Where(b => b >= zero && b <= nine)
.ToArray();
char[] numbers = Encoding.ASCII.GetChars(asciiNumbers);
// OR
string numbersString = Encoding.ASCII.GetString(asciiNumbers);
//First two number from char array
int aNum = Convert.ToInt32(numbers[0]);
int bNum = Convert.ToInt32(numbers[1]);
//First two number from string
string aString = numbersString.Substring(0,2);