using System; using System.Collections.Generic; using System.Globalization; using System.Text; using System.Text.RegularExpressions; namespace POSV.Utils { public class StringUtils { //判断字符串是否为纯数字 public static bool IsNumber(string str) { if (str == null || str.Length == 0) return false; ASCIIEncoding ascii = new ASCIIEncoding(); byte[] bytestr = ascii.GetBytes(str); foreach (byte c in bytestr) { if (c < 48 || c > 57) { return false; } } return true; } public static bool isNumberic(string message, out int result) { System.Text.RegularExpressions.Regex rex = new System.Text.RegularExpressions.Regex(@"^\d+$"); result = -1; if (rex.IsMatch(message)) { result = int.Parse(message); return true; } else { return false; } } /// /// 清除字典中值为空的项。 /// /// 待清除的字典 /// 清除后的字典 public static IDictionary CleanupDictionary(IDictionary dict) { IDictionary newDict = new Dictionary(dict.Count); IEnumerator> dem = dict.GetEnumerator(); while (dem.MoveNext()) { string name = dem.Current.Key; T value = dem.Current.Value; if (value != null) { newDict.Add(name, value); } } return newDict; } /// /// 获取文件的真实后缀名。目前只支持JPG, GIF, PNG, BMP四种图片文件。 /// /// 文件字节流 /// JPG, GIF, PNG or null public static string GetFileSuffix(byte[] fileData) { if (fileData == null || fileData.Length < 10) { return null; } if (fileData[0] == 'G' && fileData[1] == 'I' && fileData[2] == 'F') { return "GIF"; } else if (fileData[1] == 'P' && fileData[2] == 'N' && fileData[3] == 'G') { return "PNG"; } else if (fileData[6] == 'J' && fileData[7] == 'F' && fileData[8] == 'I' && fileData[9] == 'F') { return "JPG"; } else if (fileData[0] == 'B' && fileData[1] == 'M') { return "BMP"; } else { return null; } } /// /// 获取文件的真实媒体类型。目前只支持JPG, GIF, PNG, BMP四种图片文件。 /// /// 文件字节流 /// 媒体类型 public static string GetMimeType(byte[] fileData) { string suffix = GetFileSuffix(fileData); string mimeType; switch (suffix) { case "JPG": mimeType = "image/jpeg"; break; case "GIF": mimeType = "image/gif"; break; case "PNG": mimeType = "image/png"; break; case "BMP": mimeType = "image/bmp"; break; default: mimeType = "application/octet-stream"; break; } return mimeType; } /// /// 根据文件后缀名获取文件的媒体类型。 /// /// 带后缀的文件名或文件全名 /// 媒体类型 public static string GetMimeType(string fileName) { string mimeType; fileName = fileName.ToLower(); if (fileName.EndsWith(".bmp", StringComparison.CurrentCulture)) { mimeType = "image/bmp"; } else if (fileName.EndsWith(".gif", StringComparison.CurrentCulture)) { mimeType = "image/gif"; } else if (fileName.EndsWith(".jpg", StringComparison.CurrentCulture) || fileName.EndsWith(".jpeg", StringComparison.CurrentCulture)) { mimeType = "image/jpeg"; } else if (fileName.EndsWith(".png", StringComparison.CurrentCulture)) { mimeType = "image/png"; } else { mimeType = "application/octet-stream"; } return mimeType; } public static string GetString(object obj) { return GetString(obj, string.Empty); } public static string GetString(object obj, string defaultString) { return obj != null ? obj.ToString() : defaultString; } public static int GetInt(object obj) { return GetInt(obj, 0); } public static int GetInt(object obj, int defaulVale) { int _value = defaulVale; int.TryParse((obj != null ? obj.ToString() : defaulVale.ToString()), out _value); return _value; } public static double GetDouble(object obj) { return GetDouble(obj, 0); } public static double GetDouble(object obj, double defaulVale) { double _value = defaulVale; double.TryParse((obj != null ? obj.ToString() : defaulVale.ToString()), out _value); return _value; } public static decimal GetDecimal(object obj) { return GetDecimal(obj, 0); } public static decimal GetDecimal(object obj, decimal defaulVale) { decimal _value = defaulVale; decimal.TryParse((obj != null ? obj.ToString() : defaulVale.ToString()), out _value); return _value; } static Regex unicode = new Regex(@"\\u([0-9a-fA-F]{4})", RegexOptions.Compiled); public static string DecodeUnicode(string s) { return unicode.Replace(s, m => { short c; if (short.TryParse(m.Groups[1].Value, System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture, out c)) { return "" + (char)c; } return m.Value; }); } /// /// 格式化显示数字 /// /// 数字 /// 保留小数位数 /// public static string FormatDataSwitch(decimal num, int digit) { string result = string.Empty; switch (digit) { case 0: result = FormatDataNoDigit(num); break; case 1: result = FormatDataOneDigit(num); break; case 2: default: result = FormatDataTwoDigit(num); break; } return result; } public static string FormatDataNoDigit(decimal num) { return FormatData(num, "0.##"); } public static string FormatDataOneDigit(decimal num) { return FormatData(num, "0.0#"); } public static string FormatDataTwoDigit(decimal num) { return FormatData(num, "f2"); } /// /// 对象格式化两位小数 /// /// /// public static decimal FormatDataTwoDigitObject(object num) { try { return GetDecimal(FormatDataTwoDigit(GetDecimal(num))); } catch (Exception e) { return 0.00M; } } public static string FormatData(decimal num, string format) { return num.ToString(format); } /// /// 判断是否是手机号码 /// /// /// public static bool IsMobilePhone(string input) { Regex regex = new Regex("^1[3456789]\\d{9}$"); return regex.IsMatch(input); } /// /// 判断整数(int) /// /// /// public static bool IsInteger(String str) { if (string.IsNullOrEmpty(str)) { return false; } Regex regex = new Regex("^[-\\+]?[\\d]*$"); return regex.IsMatch(str); } } }