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.
153 lines
4.1 KiB
153 lines
4.1 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace Common.TextUtil
|
|
{
|
|
public static class SimpleTypeExtensions
|
|
{
|
|
/// <summary>
|
|
/// 验证字符串是否是Null或者空字符串
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
/// <returns></returns>
|
|
public static bool IsEmptyOrNull(this string str)
|
|
{
|
|
if (str == null)
|
|
{
|
|
return true;
|
|
}
|
|
else if (str == "")
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换字符串到32位整形
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
/// <returns></returns>
|
|
public static int ToInt32(this string str)
|
|
{
|
|
int rs = 0;
|
|
if (int.TryParse(str, out rs))
|
|
{
|
|
return rs;
|
|
}
|
|
throw new Exceptions.CannotConvertException(str);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换字符串到64位整形
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
/// <returns></returns>
|
|
public static long ToInt64(this string str)
|
|
{
|
|
long rs = 0;
|
|
if (long.TryParse(str, out rs))
|
|
{
|
|
return rs;
|
|
}
|
|
throw new Exceptions.CannotConvertException(str);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换字符串到16位整形
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
/// <returns></returns>
|
|
public static short ToInt16(this string str)
|
|
{
|
|
short rs = 0;
|
|
if (short.TryParse(str, out rs))
|
|
{
|
|
return rs;
|
|
}
|
|
throw new Exceptions.CannotConvertException(str);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换字符串到浮点型
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
/// <returns></returns>
|
|
public static decimal ToDecimal(this string str)
|
|
{
|
|
decimal rs = 0;
|
|
if (decimal.TryParse(str, out rs))
|
|
{
|
|
return rs;
|
|
}
|
|
throw new Exceptions.CannotConvertException(str);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换字符串到单精度浮点型
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
/// <returns></returns>
|
|
public static Single ToSingle(this string str)
|
|
{
|
|
Single rs = 0;
|
|
if (Single.TryParse(str, out rs))
|
|
{
|
|
return rs;
|
|
}
|
|
throw new Exceptions.CannotConvertException(str);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换字符串到双精度浮点型
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
/// <returns></returns>
|
|
public static double ToDouble(this string str)
|
|
{
|
|
double rs = 0;
|
|
if (double.TryParse(str, out rs))
|
|
{
|
|
return rs;
|
|
}
|
|
throw new Exceptions.CannotConvertException(str);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换字符串到布尔值,只支持0;1或者True;False,不区分大小写
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
/// <returns></returns>
|
|
public static bool ToBool(this string str)
|
|
{
|
|
if (str == "0" || str.ToUpper() == "FALSE")
|
|
{
|
|
return false;
|
|
}
|
|
else if (str == "1" || str.ToUpper() == "TRUE")
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
throw new Exceptions.CannotConvertException(str);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取字符串值,如果为空则返回空字符串,过滤%和'
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
/// <returns></returns>
|
|
public static string GetValue(this string str)
|
|
{
|
|
if (str == null)
|
|
{
|
|
return "";
|
|
}
|
|
return str.Replace("'", "‘").Replace("%", "%");
|
|
}
|
|
}
|
|
}
|
|
|