using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace Common { /// /// 字符串操作 - 工具方法 /// public static partial class StringHelper { #region ContainsChinese(是否包含中文) /// /// 是否包含中文 /// /// 文本 public static bool ContainsChinese(string text) { const string pattern = "[\u4e00-\u9fa5]+"; return Regex.IsMatch(text, pattern); } #endregion #region ContainsNumber(是否包含数字) /// /// 是否包含数字 /// /// 文本 public static bool ContainsNumber(string text) { const string pattern = "[0-9]+"; return Regex.IsMatch(text, pattern); } #endregion #region Distinct(去除重复) /// /// 去除重复 /// /// 值,范例1:"5555",返回"5",范例2:"4545",返回"45" public static string Distinct(string value) { var array = value.ToCharArray(); return new string(array.Distinct().ToArray()); } #endregion #region 删除最后一个字符之后的字符 /// /// 删除最后结尾的一个逗号 /// public static string DelLastComma(string str) { return str.Substring(0, str.LastIndexOf(",", StringComparison.Ordinal)); } /// /// 删除最后结尾的指定字符后的字符 /// public static string DelLastChar(string str, string strchar) { return str.Substring(0, str.LastIndexOf(strchar, StringComparison.Ordinal)); } /// /// 删除最后结尾的长度 /// /// /// /// public static string DelLastLength(string str, int Length) { if (string.IsNullOrEmpty(str)) return ""; str = str.Substring(0, str.Length - Length); return str; } #endregion #region 快速验证一个字符串是否符合指定的正则表达式 /// /// 快速验证一个字符串是否符合指定的正则表达式。 /// /// 正则表达式的内容。 /// 需验证的字符串。 /// 是否合法的bool值。 public static bool QuickValidate(string express, string value) { if (value == null) return false; Regex myRegex = new Regex(express); if (value.Length == 0) { return false; } return myRegex.IsMatch(value); } #endregion #region 彩票类型参数首字母转换大小写 /// /// 彩票类型参数首字母转换大小写 /// /// 字符串 /// 分割字符 /// 0:小写,1:大写 /// public static string ParamTransUperOrLower(string value, char c, int i = 0) { string result = ""; string[] arr = value.Split(c); for (int j = 0; j < arr.Length - 1; j++) { if (i == 0) { result += arr[j].ToLower() + c; } else { result += arr[j].ToUpper() + c; } } result += arr[arr.Length - 1]; return result; } /// /// 开奖日期转换 /// /// /// public static string getKjDesc(string desc) { string time = "日一二三四五六"; string[] txt = desc.Split(','); try { if (txt.Length == 7) { desc = "每天开奖一次"; } else { desc = "每"; for (var i = 0; i < txt.Length; i++) { desc += "周" + time.ElementAt(Int32.Parse(txt[i])); if (i < txt.Length - 1) { desc = desc + "、"; } } } } catch (Exception e) { return ""; } return desc; } /// /// 获取兑奖截止时间 /// /// public static DateTime GetDuiJiangtime(string OpenTime) { try { DateTime tmp = new DateTime(); DateTime.TryParse(OpenTime, out tmp); return tmp.AddDays(60); } catch (Exception ee) { return DateTime.Now.AddDays(60); } } /// /// 获取彩种去年的最后一期 /// /// /// public static int GetLastTermOfLastYear(string KJTime) { try { var kjtime = KJTime.Split(','); DateTime time = DateTime.Now; int year = time.Year - 1; DateTime etime = new DateTime(year, 12, 31); DateTime stime = new DateTime(year, 1, 1); var tp = etime - stime; int caday = tp.Days; int termcount = 0; for (int i = 0; i < caday; i++) { stime = stime.AddDays(1); if (kjtime.Contains(((int)stime.DayOfWeek).ToString())) { termcount += 1; } } return termcount; } catch (Exception ee) { return 0; } } public static int GetLastYear(string qs) { int lastyear = 0; try { if (qs.Length == 7) { lastyear = int.Parse(qs.Substring(0, 4)) - 1; } else { lastyear = int.Parse("20" + qs.Substring(0, 2)) - 1; } return lastyear; } catch (Exception ee) { return 0; } } #endregion } }