123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace Common
- {
- /// <summary>
- /// 字符串操作 - 工具方法
- /// </summary>
- public static partial class StringHelper
- {
- #region ContainsChinese(是否包含中文)
- /// <summary>
- /// 是否包含中文
- /// </summary>
- /// <param name="text">文本</param>
- public static bool ContainsChinese(string text)
- {
- const string pattern = "[\u4e00-\u9fa5]+";
- return Regex.IsMatch(text, pattern);
- }
- #endregion
- #region ContainsNumber(是否包含数字)
- /// <summary>
- /// 是否包含数字
- /// </summary>
- /// <param name="text">文本</param>
- public static bool ContainsNumber(string text)
- {
- const string pattern = "[0-9]+";
- return Regex.IsMatch(text, pattern);
- }
- #endregion
- #region Distinct(去除重复)
- /// <summary>
- /// 去除重复
- /// </summary>
- /// <param name="value">值,范例1:"5555",返回"5",范例2:"4545",返回"45"</param>
- public static string Distinct(string value)
- {
- var array = value.ToCharArray();
- return new string(array.Distinct().ToArray());
- }
- #endregion
- #region 删除最后一个字符之后的字符
- /// <summary>
- /// 删除最后结尾的一个逗号
- /// </summary>
- public static string DelLastComma(string str)
- {
- return str.Substring(0, str.LastIndexOf(",", StringComparison.Ordinal));
- }
- /// <summary>
- /// 删除最后结尾的指定字符后的字符
- /// </summary>
- public static string DelLastChar(string str, string strchar)
- {
- return str.Substring(0, str.LastIndexOf(strchar, StringComparison.Ordinal));
- }
- /// <summary>
- /// 删除最后结尾的长度
- /// </summary>
- /// <param name="str"></param>
- /// <param name="Length"></param>
- /// <returns></returns>
- public static string DelLastLength(string str, int Length)
- {
- if (string.IsNullOrEmpty(str))
- return "";
- str = str.Substring(0, str.Length - Length);
- return str;
- }
- #endregion
- #region 快速验证一个字符串是否符合指定的正则表达式
- /// <summary>
- /// 快速验证一个字符串是否符合指定的正则表达式。
- /// </summary>
- /// <param name="express">正则表达式的内容。</param>
- /// <param name="value">需验证的字符串。</param>
- /// <returns>是否合法的bool值。</returns>
- 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 彩票类型参数首字母转换大小写
- /// <summary>
- /// 彩票类型参数首字母转换大小写
- /// </summary>
- /// <param name="value">字符串</param>
- /// <param name="c">分割字符</param>
- /// <param name="i">0:小写,1:大写</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 开奖日期转换
- /// </summary>
- /// <param name="desc"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 获取兑奖截止时间
- /// </summary>
- /// <returns></returns>
- 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);
- }
- }
- /// <summary>
- /// 获取彩种去年的最后一期
- /// </summary>
- /// <param name="KJTime"></param>
- /// <returns></returns>
- 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
- }
- }
|