C# 提取字符串中的数字


一、使用正则表达式

1 string str = "sztq数字提取123sztq数字提取";
2 string result = System.Text.RegularExpressions.Regex.Replace(str, @"[^0-9]+", "");
3 Console.WriteLine("使用正则表达式提取");
4 Console.WriteLine(result);


二、使用ASCII码

 1  string str = "sztq数字提取123sztq数字提取";
 2  foreach (char c in str)
 3          {
 4              if (Convert.ToInt32(c) >= 48 && Convert.ToInt32(c) <= 57)
 5               {
 6                   sb.Append(c);
 7               }
 8          }
 9  Console.WriteLine("使用ASCII码提取");
10  Console.WriteLine(sb.ToString());

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM