使用正则表达式
string str = "sztq数字提取123sztq数字提取";
string result = System.Text.RegularExpressions.Regex.Replace(str, @"[^0-9]+", "");
//System.Text.RegularExpressions.Regex.Replace(_项目原始值, @"[^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());
去除字符串中重复字符,以逗号分隔
string s = "sdjfasdfjjlasdhfueiuhjfklhasdlkfjhasdjfasdlk";
IEnumerable<char> distinctList = s.Distinct();
string join = String.Join(",", distinctList);