You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

282 lines
7.3 KiB

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
{
/// <summary>
/// 取得从表单传递的内容
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 取得从表单传递的数字
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static int GetID(string content)
{
int ret = 0;
if (content != "")
{
if (IsNumeric(content))
{
ret = Convert.ToInt32(content);
}
}
return ret;
}
/// <summary>
/// 判断是否为正整数
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 判断是否是日期时间格式
/// </summary>
/// <param name="Date"></param>
/// <returns></returns>
public static bool IsDateTime(string Date)
{
try
{
Convert.ToDateTime(Date);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 格式化时间/时间
/// </summary>
/// <param name="Type"></param>
/// <param name="dt"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 转换日期/时间
/// </summary>
/// <param name="datetime"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 剪切指定长度的文本
/// </summary>
/// <param name="str"></param>
/// <param name="len"></param>
/// <returns></returns>
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("<", "&lt;");
str = str.Replace(">", "&gt;");
str = str.Replace("\r\n", "<br>");
str = str.Replace(" ", "&nbsp;");
return str;
}
public static string AddHtmlBr(string str)
{
str = str.Replace("\r\n", "<br>");
return str;
}
/// <summary>
/// 根据日期、时间取得一个订单号
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
/// 在指定字串右边填充指定的字串
/// </summary>
/// <param name="o_str">要填充的原字串</param>
/// <param name="p_str">填充的字符</param>
/// <param name="len">要填充的长度</param>
/// <returns></returns>
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;
}
}
}