Regex regex = new System.Text.RegularExpressions.Regex("^(-?[0-9]*[.]*[0-9]{0,3})$");ide
string itemValue="abc123"spa
bool b=regex.IsMatch(itemValue); code
if(b==true)blog
{字符串
//是純數字string
}it
elseio
{event
//不是純數字class
}
下面這段代碼是判斷是否爲純數字,若是是就加1,若是是以字母開頭的數字字符串,字母不變,後面數字加1
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 string str = Console.ReadLine(); 6 string strTmp = ""; 7 if (IsNumeric(str)) 8 { 9 strTmp = (ConvertToLong(Convert.ToInt32(str) + 1).ToString().PadLeft(str.Length, '0')); 10 } 11 else 12 { 13 int iNum = str.Length; 14 int j = 0; 15 for (int i = 0; i < iNum; i++) 16 { 17 if (!IsNumeric(str[i].ToString())) 18 { 19 j++; 20 } 21 } 22 strTmp = str.Substring(0, j) + (ConvertToLong(Convert.ToInt32(str.Substring(j, str.Length - j)) + 1).ToString().PadLeft(str.Substring(j, str.Length - j).Length, '0')); 23 } 24 Console.WriteLine(strTmp); 25 Console.ReadKey(); 26 } 27 public static bool IsNumeric(string itemValue) 28 { 29 return (IsRegEx("^(-?[0-9]*[.]*[0-9]{0,3})$", itemValue)); 30 } 31 public static long ConvertToLong(object value) 32 { 33 if (value == null || value.ToString().Trim() == "") 34 { 35 return 0; 36 } 37 38 long nValue; 39 long.TryParse(value.ToString(), out nValue); 40 41 return nValue; 42 } 43 public static bool IsRegEx(string regExValue, string itemValue) 44 { 45 try 46 { 47 Regex regex = new System.Text.RegularExpressions.Regex(regExValue); 48 if (regex.IsMatch(itemValue)) return true; 49 else return false; 50 } 51 catch (Exception) 52 { 53 return false; 54 } 55 finally 56 { 57 } 58 } 59 }