/// <summary>转载:www.uzhanbao.com /// 判断是否十六进制格式字符串 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsHexadecimal(string str) { const string PATTERN = @"[A-Fa-f0-9]+$"; bool bo = System.Text.RegularExpressions.Regex.IsMatch(str, PATTERN); if(bo == true){ // 长度判断 长度+1 与3的余数为0表示长度符合要求 if((str.Length+1) % 3 != 0){ bo = false; } else{ // 空格判断,空格数为长度+1 与3的余数 if (str.Length > 2) { string[] arr = str.Split(new char[] { ' ' }); //分离后,得到空格数 int space_count = arr.Length - 1; //实际空格数 int n = ((str.Length + 1) / 3) -1; // 获得字符串中所有的空格数 int cout = Regex.Matches(str, @" ").Count; if (space_count != n || cout != space_count) { bo = false; } else { for (int i = 0; i < str.Length; i++) { if ((i + 1) % 3 == 0) { // 判断 2 5 8 11 .. 位置是否为空格 if (str[i] != ' ') { bo = false; break; } } } } } else if (str.Length < 2) { bo = false; } } } return bo; }?