how does C # detect what numbers are in a string string?
Test function private void Form1_Load (object sender, EventArgs e) { foreach (var number in Extractnumbersfromstring ("abc2345 345fdf678 jdhfg945")) { MessageBox.Show (number. ToString ()); } } Private Ienumerable<int> extractnumbersfromstring (string s) { //regex.matches Method: Searches the input string for all occurrences of the regular expression and returns all matches. matches the preceding character or sub-expression one or more times. Equivalent to {1,}. If the + is removed, it is //return regex.matches (S, @ "\d+"). Cast<match> (). Select (M = Convert.ToInt32 (M.value)); Return Regex.Matches (S, @ "\d"). Cast<match> (). Select (M = Convert.ToInt32 (M.value)); }
"Abc2345 345fdf678 jdhfg945"
The above example test, there is a plus, this output:
2345 345678945
When there is no +, the output is this:
23453 .....
The above is how C # detects what numbers are in string strings? For more information, please pay attention to topic.alibabacloud.com (www.php.cn)!