using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace CK.SCP.Common { public class MyWebString { /// /// 取得从表单传递的内容 /// /// /// public static string GetString(string content) { string ret = ""; ret = content.Replace("'", "''"); return ret; } public static string GetString(string content, int length) { string ret = ""; ret = ret.Substring(0, length); ret = content.Replace("'", "''"); return ret; } /// /// 取得从表单传递的数字 /// /// /// public static int GetID(string content) { int ret = 0; if (content != "") { if (IsNumeric(content)) { ret = Convert.ToInt32(content); } } return ret; } /// /// 判断是否为正整数 /// /// /// public static bool IsNumeric(string str) { bool ret; if (str != "") { if (str != null && Regex.IsMatch(str, @"^\d+$")) ret = true; else ret = false; } else { ret = true; } return ret; } /// /// 判断是否是日期时间格式 /// /// /// public static bool IsDateTime(string Date) { try { Convert.ToDateTime(Date); return true; } catch { return false; } } /// /// 格式化时间/时间 /// /// /// /// public static string FormatDateTime(int Type, string dt) { string result = ""; if (dt == null || dt == "") { return result; } DateTime _dt = Convert.ToDateTime(dt); int Year = _dt.Year; int Month = _dt.Month; int Day = _dt.Day; string sYear = Year.ToString(); string sMonth = Month.ToString(); string sDay = Day.ToString(); if (Month < 10) sMonth = "0" + Month.ToString(); if (Day < 10) sDay = "0" + Day.ToString(); switch (Type) { case 1: //06-03 result = sMonth + "-" + sDay; break; case 2: //2010-10-01 result = sYear + "-" + sMonth + "-" + sDay; break; case 3: //2010年10月01日 result = sYear + "年" + sMonth + "月" + sDay + "日"; break; case 4://20100310" result = sYear + "" + sMonth + "" + sDay + ""; break; } return result; } public static string GetBatch() { string result = ""; DateTime _dt = DateTime.Now; int Year = _dt.Year; int Month = _dt.Month; int Day = _dt.Day; string sYear = Year.ToString(); string sMonth = Month.ToString(); string sDay = Day.ToString(); if (Month < 10) sMonth = "0" + Month.ToString(); if (Day < 10) sDay = "0" + Day.ToString(); sYear = sYear.Substring(2, 2); result = sYear + sMonth + sDay; return result; } /// /// 转换日期/时间 /// /// /// public static DateTime ConvertTime(string datetime) { DateTime dt; datetime = datetime.Trim(); if (datetime == "") throw new Exception("时间/日期格式不正确!"); try { dt = Convert.ToDateTime(datetime); } catch { throw new Exception("[" + datetime + "]时间/日期格式不正确!"); } return dt; } public static string Week(string datetime) { DateTime dt = Convert.ToDateTime(datetime); string[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; string week = weekdays[Convert.ToInt32(dt.DayOfWeek)]; return week; } /// /// 剪切指定长度的文本 /// /// /// /// public static string CutString(string str, int len) { if (str.Length <= len) return str; str = str.Substring(0, len) + "..."; return str; } public static string CleanHtmlBr(string str) { str = str.Replace("<", "<"); str = str.Replace(">", ">"); str = str.Replace("\r\n", "
"); str = str.Replace(" ", " "); return str; } public static string AddHtmlBr(string str) { str = str.Replace("\r\n", "
"); return str; } /// /// 根据日期、时间取得一个订单号 /// /// public static string GetOrderNumber() { string ret = ""; //Random r = new Random(DateTime.Now.Millisecond); //int ex = r.Next(1000, 9999); //ret = DateTime.Now.ToString("yyMMddHHmmss") + "-" + ex; ret = Guid.NewGuid().ToString(); return ret; } /// /// 在指定字串右边填充指定的字串 /// /// 要填充的原字串 /// 填充的字符 /// 要填充的长度 /// public static string PadLeftString(string o_str, char p_str, int len) { string result = ""; if (o_str.Length > len) { //result = o_str.Substring(0, len); result = o_str; } else { int slen = len - o_str.Length; result = o_str; while (slen > 0) { result = p_str.ToString() + result; slen--; } } return result; } } }