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