一、使用正则表达式
string str = "sztq数字提取123sztq数字提取";
string result = System.Text.RegularExpressions.Regex.Replace(str, @"[^0-9]+", "");
Console.WriteLine("使用正则表达式提取");
Console.WriteLine(result);
二、使用ASCII码
string str = "sztq数字提取123sztq数字提取";
foreach (char c in str)
{
if (Convert.ToInt32(c) >= 48 && Convert.ToInt32(c) <= 57)
{
sb.Append(c);
}
}
Console.WriteLine("使用ASCII码提取");
Console.WriteLine(sb.ToString());
提取字符串中的数字
最新推荐文章于 2023-06-22 12:52:57 发布