CheckData字符类方法(字符串操作)(5)
来源:互联网 发布:bi和数据分析 编辑:程序博客网 时间:2024/06/09 18:54
/// <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 "";
}
///
/// </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 "";
}
- CheckData字符类方法(字符串操作)(5)
- CheckData字符类方法(字符串操作)(1)
- CheckData字符类方法(字符串操作)(2)
- CheckData字符类方法(字符串操作)(3)
- CheckData字符类方法(字符串操作)(4)
- CheckData字符类方法(字符串操作)(6)
- 字符和字符串操作
- php 字符串操作 字符 php
- 字符及字符串操作函数
- Standard_C_字符和字符串操作
- c字符和字符串操作
- 字符/字符串操作函数(C)
- 字符及字符串操作函数
- 第十三周项目5-字符串操作:字符统计
- 第13周项目5-字符串操作--字符统计
- ECMA 字符串操作 方法
- 操作字符串方法
- c#操作字符串方法
- CheckData字符类方法(字符串操作)(1)
- 监控函数的执行时间,若超时则强制返回一个默认值
- CheckData字符类方法(字符串操作)(2)
- CheckData字符类方法(字符串操作)(3)
- CheckData字符类方法(字符串操作)(4)
- CheckData字符类方法(字符串操作)(5)
- CheckData字符类方法(字符串操作)(6)
- flex 屏蔽鼠标右键
- 今天来csdn blog了,
- 新手入门 Linux菜鸟必学的60个命令
- C#列出局域网中可用SQL Server服务器
- IDC发布2007年存储行业十大预测
- 在C#中如何实现Form与Form之间的通信
- new和malloc的区别