using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace CY_DocumentSynchroWCFService
{
public class TypeParse
{
///
/// 判断对象是否为Int32类型的数字
///
///
///
public static bool IsNumeric(object expression)
{
if (expression != null)
{
return IsNumeric(expression.ToString());
}
return false;
}
///
/// 判断对象是否为Int32类型的数字
///
///
///
public static bool IsNumeric(string expression)
{
if (expression != null)
{
string str = expression;
if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
{
if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
{
return true;
}
}
}
return false;
}
///
/// 是否为Double类型
///
///
///
public static bool IsDouble(object expression)
{
if (expression != null)
{
return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");
}
return false;
}
///
/// string型转换为bool型
///
/// 要转换的字符串
/// 缺省值
/// 转换后的bool类型结果
public static bool StrToBool(object expression, bool defValue)
{
if (expression != null)
{
return StrToBool(expression, defValue);
}
return defValue;
}
///
/// string型转换为bool型
///
/// 要转换的字符串
/// 缺省值
/// 转换后的bool类型结果
public static bool StrToBool(string expression, bool defValue)
{
if (expression != null)
{
if (string.Compare(expression, "true", true) == 0)
{
return true;
}
else if (string.Compare(expression, "false", true) == 0)
{
return false;
}
}
return defValue;
}
///
/// 将对象转换为Int32类型
///
/// 要转换的字符串
/// 缺省值
/// 转换后的int类型结果
public static int StrToInt(object expression, int defValue)
{
if (expression != null)
{
return StrToInt(expression.ToString(), defValue);
}
return defValue;
}
///
/// 将对象转换为Int32类型
///
/// 要转换的字符串
/// 缺省值
/// 转换后的int类型结果
public static int StrToInt(string str, int defValue)
{
if (string.IsNullOrEmpty(str) || str.Trim().Length >= 11 || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
return defValue;
int rv;
if (Int32.TryParse(str, out rv))
return rv;
return Convert.ToInt32(StrToFloat(str, defValue));
}
///
/// string型转换为float型
///
/// 要转换的字符串
/// 缺省值
/// 转换后的int类型结果
public static float StrToFloat(object strValue, float defValue)
{
if ((strValue == null))
{
return defValue;
}
return StrToFloat(strValue.ToString(), defValue);
}
///
/// string型转换为float型
///
/// 要转换的字符串
/// 缺省值
/// 转换后的int类型结果
public static float StrToFloat(string strValue, float defValue)
{
if ((strValue == null) || (strValue.Length > 10))
{
return defValue;
}
float intValue = defValue;
if (strValue != null)
{
bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
if (IsFloat)
{
float.TryParse(strValue, out intValue);
}
}
return intValue;
}
///
/// 判断给定的字符串数组(strNumber)中的数据是不是都为数值型
///
/// 要确认的字符串数组
/// 是则返加true 不是则返回 false
public static bool IsNumericArray(string[] strNumber)
{
if (strNumber == null)
{
return false;
}
if (strNumber.Length < 1)
{
return false;
}
foreach (string id in strNumber)
{
if (!IsNumeric(id))
{
return false;
}
}
return true;
}
}
}