/// <summary>
/// 类拟substring一样eg:GetCountString("abcde",1,3) 返回bcd
/// </summary>
/// <param name="lzpstr"></param>
/// <param name="StartIndex"></param>
/// <param name="length"></param>
/// <returns></returns>
public string GetCountString(string lzpstr, int StartIndex, int length)
{
string s = "";
byte[] bytes = Encoding.Default.GetBytes(lzpstr);
if (StartIndex > bytes.Length)
{
return "";
}
if (length > (bytes.Length - StartIndex))
{
length = bytes.Length - StartIndex;
}
s = Encoding.Default.GetString(bytes, StartIndex, length);
if (Encoding.Default.GetBytes(s).Length < length)
{
s = Encoding.Default.GetString(bytes, StartIndex, length + 1);
}
return s;
}
/// <summary>
/// 返回分隔符最后一个字串 eg:GetShortFileName("ab,cd,efg",',') 返回efg
/// </summary>
/// <param name="FileName"></param>
/// <param name="spliter"></param>
/// <returns></returns>
public string GetShortFileName(string FileName, char spliter)
{
string[] textArray = FileName.Split(new char[] { spliter });
return textArray[textArray.Length - 1].ToString();
}
/// <summary>
/// 返回分隔符最后一个字串 跟 GetShortFileName类拟 默认以 . 为分隔符 eg:GetTailName("abc.defgh.ijh") 返回 ijh
/// </summary>
/// <param name="FileName"></param>
/// <returns></returns>
public string GetTailName(string FileName)
{
string[] textArray = FileName.Split(new char[] { '.' });
return textArray[textArray.Length - 1].ToString();
}
/// <summary>
/// 返回当前时间的秒eg:20070525020038 会随时间而变化
/// </summary>
/// <returns></returns>
public string GetUniqueFileName()
{
return DateTime.Now.ToString("yyyyMMddhhmmss");
}
/// <summary>
/// 默认:字符补在右边
/// </summary>
/// <param name="str"></param>
/// <param name="width"></param>
/// <param name="compart"></param>
/// <returns></returns>
public static string FormatStr(string str, int width, string compart)
{
return FormatStr(str, width, compart, false);
}
/// <summary>
/// 格式化,返回定长字串
/// </summary>
/// <param name="str"></param>
/// <param name="width">字串宽度</param>
/// <param name="compart">不够宽度时补上的字符</param>
/// <param name="isLeft">左对齐?default:true 补在右边 </param>
/// <returns></returns>
public static string FormatStr(string str, int width, string compart, bool isLeft)
{
int byteCount = Encoding.Default.GetByteCount(str);
string text = "";
for (int i = 0; i < (width - byteCount); i++)
{
text = text + compart;
}
if (isLeft)
{
return (text + str);
}
return (str + text);
}
/// <summary>
/// 根据文件扩展名取得文件类型
/// </summary>
/// <param name="extend"></param>
/// <returns></returns>
private static string getFileType(string extend)
{
switch (extend.ToLower())
{
case ".jpg":
case ".jpeg":
return "image/pjpeg";
case ".gif":
return "image/gif";
case ".png":
return "image/x-png";
case ".bmp":
return "image/bmp";
case ".txt":
return "text/plain";
case ".htm":
case ".html":
return "text/html";
}
return "";
}
/// <summary>
/// 字符串编码
/// </summary>
/// <param name="inputData"></param>
/// <returns></returns>
public static string HtmlEncode(string inputData)
{
return HttpUtility.HtmlEncode(inputData);
}
/// <summary>
/// 检查邮件地址
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsEmail(string inputData)
{
return RegEmail.Match(inputData).Success;
}
/// <summary>
/// 是否为空
/// </summary>
/// <param name="key">目标物件 </param>
/// <returns></returns>
public static bool IsEmpty(object key)
{
return !ToType(key);
}
/// <summary>
/// 检测是否有中文字符
/// </summary>
/// <param name="inputData"></param>
/// <returns></returns>
public static bool IsHasCHZN(string inputData)
{
return RegCHZN.Match(inputData).Success;
}
/// <summary>
/// 是否为图片类型 默认值: ".jpg",".gif",".bmp",".png"
/// </summary>
/// <param name="imgType">File.ContentType</param>
/// <returns></returns>
public static bool IsImage(string imgType)
{
return IsImage(imgType, new string[] { ".jpg", ".gif", ".bmp", ".png" });
}
/// <summary>
/// 是否为图片类型
/// </summary>
/// <param name="imgType">File.ContentType</param>
/// <param name="allowType">允许的文件扩展名 eg: ".jpg",".gif"</param>
/// <returns></returns>
public static bool IsImage(string imgType, params string[] allowType)
{
imgType = imgType.ToLower();
for (int i = 0; i < allowType.Length; i++)
{
if (imgType == getFileType(allowType[i]))
{
return true;
}
}
return false;
}
/// <summary>
/// 是否数字字符串 [0-9]
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsNumber(string inputData)
{
return RegNumber.Match(inputData).Success;
}
/// <summary>
/// 是否数字字符串 可带正负号 [0-9]
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsNumberSign(string inputData)
{
return RegNumberSign.Match(inputData).Success;
}
/// <summary>
/// 检查字符串最大长度,返回指定长度的串
/// </summary>
/// <param name="sqlInput"></param>
/// <param name="maxLength"></param>
/// <returns></returns>
public static string SqlText(string sqlInput, int maxLength)
{
if ((sqlInput != null) && (sqlInput != string.Empty))
{
sqlInput = sqlInput.Trim();
if (sqlInput.Length > maxLength)
{
sqlInput = sqlInput.Substring(0, maxLength);
}
}
return sqlInput;
}
/// <summary>
/// 检查从页面输入的字符串,防止SQL注入攻击
/// </summary>
/// <param name="Sour"></param>
/// <returns></returns>
public static string CheckString(string Sour)
{
return Sour.Replace("'", "''").Replace("--", "- - ");
}
/// <summary>
/// 比较两个 double 值 的大小返回 1|0|-1
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static int CompareDouble(double a, double b)
{
if (Math.Abs((double) (a - b)) <= 1E-06)
{
return 0;
}
if (a > b)
{
return 1;
}
return -1;
}
/// <summary>
/// if true then return 1 else return 0
/// </summary>
/// <param name="isTrue"></param>
/// <returns></returns>
public static string ConvertBoolToString(bool isTrue)
{
if (isTrue)
{
return "1";
}
return "0";
}
/// <summary>
/// 将空值转换为"0"
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string ConvertEmptyTo0(object key)
{
if (object.Equals(key, null))
{
return "0";
}
if (key.ToString() == "")
{
return "0";
}
return key.ToString();
}
/// <summary>
/// if Value is null or "" ,return "0"
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string ConvertEmptyTo0(string key)
{
if ((key != null) && (key != ""))
{
return key;
}
return "0";
}
/// <summary>
/// if 1 then return true else return false
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool ConvertStringToBool(string key)
{
return (key == "1");
}
/// <summary>
/// 返回有两位小数的字串,eg: 1,234,567.89 为0则返回空
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public static string FormatDouble(double num)
{
if (num == 0)
{
return "";
}
return string.Format("{0:N2}", num);
}
/// <summary>
/// 返回有两位小数的字串 eg: 1,234,567.89 为0则返回 0.00,且无千位分隔符
/// </summary>
/// <param name="num"></param>
/// <param name="isZeroToEmpty">是否将"0"转成"",Default:true</param>
/// <returns></returns>
public static string FormatDouble(object num, bool isZeroToEmpty)
{
if (isZeroToEmpty)
{
return FormatDouble(num);
}
try
{
return string.Format("{0:F2}", ToDouble(num));
}
catch
{
return ToString(num);
}
}
/// <summary>
/// 返回有两位小数的字串 eg: 1,234,567.89 为0则返回空
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public static string FormatDouble(object num)
{
try
{
return FormatDouble(ToDouble(num));
}
catch
{
return ToString(num);
}
}
/// <summary>
///
/// </summary>
/// <param name="Sour"></param>
/// <returns></returns>
private static string CheckDate2(string Sour)
{
string text = "";
if (Sour == "")
{
return "日期不能为空";
}
if (Sour.CompareTo(DateTime.Now.ToString("yyyy-MM-dd")) < 0)
{
return "日期不能小于当前日期";
}
Match match = new Regex("^([0-9]{4})-([0-9]{2})-([0-9]{2})$").Match(Sour);
if (match.Success)
{
int year = Convert.ToInt32(match.Groups[1].Value);
int month = Convert.ToInt32(match.Groups[2].Value);
int day = Convert.ToInt16(match.Groups[3].Value);
try
{
new DateTime(year, month, day);
}
catch
{
text = "日期格式不正确,应为【2002-09-06】";
}
return text;
}
return "日期格式不正确,应为【2002-09-06】";
}
/// <summary>
/// 是否为数字,一组数字和对应的一组错误信息
/// </summary>
/// <param name="values">Values</param>
/// <param name="texts">错误信息</param>
/// <returns></returns>
public static string CheckDouble(string[] values, string[] texts)
{
string text = "";
int index = 0;
for (index = 0; index < values.Length; index++)
{
if (!IsDecimal(values[index]))
{
text = text + texts[index] + "必须为数字//n";
}
}
return text;
}
/// <summary>
/// 简体中文环境下检查日期格式是否正确 接受 8位或10位的字串,其它位皆为 false
/// </summary>
/// <param name="sour">20060514 or 2005-05-14 等 8位或10位日期字串</param>
/// <returns>Return Value</returns>
public static bool RegDateCHS(string sour)
{
string text = "";
if (sour.Length == 8)
{
text = sour.Substring(0, 4) + "-" + sour.Substring(4, 2) + "-" + sour.Substring(6, 2);
}
else if (sour.Length == 10)
{
text = sour.Substring(0, 4) + "-" + sour.Substring(5, 2) + "-" + sour.Substring(8, 2);
}
if (text == "")
{
return false;
}
try
{
Convert.ToDateTime(text);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 是否是浮点数
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsDecimal(string inputData)
{
return RegDecimal.Match(inputData).Success;
}
/// <summary>
/// 是否是浮点数 可带正负号
/// </summary>
/// <param name="inputData"></param>
/// <returns></returns>
public static bool IsDecimalSign(string inputData)
{
return RegDecimalSign.Match(inputData).Success;
}
/// <summary>
/// 比较日期,必须为10位 2002/09/15格式
/// </summary>
/// <param name="Sour">待测试的日期字串 </param>
/// <returns>ture or false</returns>
private static bool RegDate(string Sour)
{
Match match = new Regex(@"^([0-9]{4})//([0-9]{2})//([0-9]{2})$").Match(Sour);
bool flag = true;
if (match.Success)
{
int year = Convert.ToInt32(match.Groups[1].Value);
int month = Convert.ToInt32(match.Groups[2].Value);
int day = Convert.ToInt16(match.Groups[3].Value);
try
{
new DateTime(year, month, day);
}
catch
{
flag = false;
}
return flag;
}
return false;
}
/// <summary>
/// 如果为空,return false, else return true;
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
private static bool ToType(object key)
{
if (key == null)
{
return false;
}
if (key == DBNull.Value)
{
return false;
}
if (key.ToString() == string.Empty)
{
return false;
}
if (key.ToString().Trim() == "")
{
return false;
}
if (key.ToString().ToLower() == " ")
{
return false;
}
return true;
}
/// <summary>
/// 转换为String
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string ToString(object key)
{
if (ToType(key))
{
return key.ToString();
}
return "";
}
#region // Fields
private static Regex RegDecimal;
private static Regex RegCHZN;
private static Regex RegDecimalSign;
private static Regex RegEmail;
private static Regex RegNumber;
private static Regex RegNumberSign;
#endregion
#region // Methods
/// <summary>
/// 构造函数
/// </summary>
public CheckData()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
static CheckData()
{
RegNumber = new Regex("^[0-9]+$");
RegNumberSign = new Regex("^[+-]?[0-9]+$");
RegDecimal = new Regex("^[0-9]+[.]?[0-9]*$");
RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]*$");
RegEmail = new Regex(@"^[/w-]+@[/w-]+/.(com|cn|net|org|edu|mil|tv|biz|info)$");
RegCHZN = new Regex("[/u4e00-/u9fa5]");
}
/// <summary>
/// 如果格式正确则返回空,否则返回错误信息适用于 2006/06/13 格式
/// </summary>
/// <param name="Sour">待测试的日期字串</param>
/// <returns></returns>
public static string CheckDate(string Sour)
{
return CheckDate(Sour, true, true);
}
/// <summary>
/// 如果格式正确则返回空,否则返回错误信息
/// </summary>
/// <param name="Sour">待测试的日期字串</param>
/// <param name="IsUpToday">是否必须大于今天</param>
/// <param name="IsNotNull">是否不可以为空值</param>
/// <returns></returns>
public static string CheckDate(string Sour, bool IsUpToday, bool IsNotNull)
{
if (IsNotNull)
{
if (Sour == "")
{
return "日期不能为空";
}
}
else if (Sour == "")
{
return "";
}
if (IsUpToday && (Sour.CompareTo(DateTime.Now.ToString(@"yyyy//MM//dd")) < 0))
{
return "日期不能小于当前日期";
}
if (!RegDate(Sour))
{
return "日期格式不正确,应为【2002/09/06】";
}
return "";
}
/// <summary>
/// 如果格式正确则返回空,否则返回错误信息
/// </summary>
/// <param name="Sour">待测试的日期字串</param>
/// <param name="IsUpToday">是否必须大于今天</param>
/// <returns></returns>
public static string CheckDate(string Sour, bool IsUpToday)
{
return CheckDate(Sour, IsUpToday, true);
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。