123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace WebAPIBase.Utils
- {
- public static partial class ExtensionMethod
- {
- #region string判断扩展
- public static bool IsNullOrEmpty(this string str)
- {
- return StringHelper.IsNullOrEmpty(str);
- }
- public static bool IsNotNullAndEmpty(this string str)
- {
- return StringHelper.IsNotNullAndEmpty(str);
- }
- /// <summary>
- /// 判断是否是整数
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool IsInteger(this string str)
- {
- return StringHelper.IsInteger(str);
- }
- /// <summary>
- /// 判断字符串是否是正整数,可以是无数位
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool IsPositiveInteger(this string str)
- {
- return StringHelper.IsPositiveInteger(str);
- }
- /// <summary>
- /// 判断是否为decimal类型
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool IsDecimal(this string str)
- {
- return StringHelper.IsDecimal(str);
- }
- public static bool IsNumber(this string str)
- {
- return StringHelper.IsNumber(str);
- }
- /// <summary>
- /// 判断是否为手机号
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool IsCellPhone(this string str)
- {
- return StringHelper.IsCellPhone(str);
- }
- /// <summary>
- /// 是否固定电话
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool IsPhone(this string str)
- {
- return StringHelper.IsPhone(str);
- }
- /// <summary>
- /// 判断是否为邮箱地址
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool IsEmail(this string str)
- {
- return StringHelper.IsEmail(str);
- }
- /// <summary>
- /// 判断身份证号格式是否正确
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool IsIDCard(this string str)
- {
- return StringHelper.IsIDCard(str);
- }
- #endregion
- #region object 判断扩展
- /// <summary>
- /// 是null或空
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static bool IsNullOrEmpty(this object obj)
- {
- return obj == null ? true : StringHelper.IsNullOrEmpty(obj.ToString());
- }
- public static bool IsNotNullAndEmpty(this object obj)
- {
- return obj == null ? false : StringHelper.IsNotNullAndEmpty(obj.ToString());
- }
- public static bool IsInteger(this object obj)
- {
- return obj == null ? false : StringHelper.IsInteger(obj.ToString());
- }
- public static bool IsDecimal(this object obj)
- {
- return obj == null ? false : StringHelper.IsDecimal(obj.ToString());
- }
- public static bool IsNumber(this object obj)
- {
- return obj == null ? false : StringHelper.IsNumber(obj.ToString());
- }
- /// <summary>
- /// 对象转换为字符串,如果对象为null,则为空
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static string SConvertString(this object obj)
- {
- return StringHelper.SConvertString(obj);
- }
- public static DataSet ToDataSet<T>(this IList<T> list)
- {
- Type elementType = typeof(T);
- var ds = new DataSet();
- var t = new DataTable();
- ds.Tables.Add(t);
- elementType.GetProperties().ToList().ForEach(propInfo => t.Columns.Add(propInfo.Name, Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType));
- foreach (T item in list)
- {
- var row = t.NewRow();
- elementType.GetProperties().ToList().ForEach(propInfo => row[propInfo.Name] = propInfo.GetValue(item, null) ?? DBNull.Value);
- t.Rows.Add(row);
- }
- return ds;
- }
- #endregion
- #region 转换扩展
- /// <summary>
- /// 转换为int32类型,若是非数值型字串,则变为默认值0
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static int ToInteger(this object str)
- {
- try
- {
- return Convert.ToInt32(str);
- }
- catch
- {
- return 0;
- }
- }
- /// <summary>
- /// 转换为Double类型,若是非数值型字串,则变为默认值0
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static double ToNumber(this object str)
- {
- try
- {
- return Convert.ToDouble(str);
- }
- catch
- {
- return 0;
- }
- }
- /// <summary>
- /// 转换为Decimal类型,若是非数值型字串,则变为默认值0
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static decimal ToDecimal(this object str)
- {
- try
- {
- return Convert.ToDecimal(str);
- }
- catch
- {
- return 0;
- }
- }
- public static DateTime ToDateTime(this string str)
- {
- return DateTime.Parse(str);
- }
- public static string FormatWith(this string str, params object[] objs)
- {
- return string.Format(str, objs);
- }
- /// <summary>
- /// 首字母小写
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- ///
- public static string ToCamel(this string str)
- {
- if (str.IsNullOrEmpty())
- {
- return str;
- }
- else
- {
- if (str.Length > 1)
- {
- return str[0].ToString().ToLower() + str.Substring(1);
- }
- else
- {
- return str.ToLower();
- }
- }
- }
- /// <summary>
- ///首字母大写
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static string ToPascal(this string str)
- {
- if (str.IsNullOrEmpty())
- {
- return str;
- }
- else
- {
- if (str.Length > 1)
- {
- return str[0].ToString().ToUpper() + str.Substring(1);
- }
- else
- {
- return str.ToLower();
- }
- }
- }
- /// <summary>
- /// 得到大写
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static string To32MD5(this string str)
- {
- return StringHelper.EncryptMD5(str);
- }
- /// <summary>
- /// 得到小写
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static string To16MD5(this string str)
- {
- return StringHelper.ASP16MD5(str);
- }
- #endregion
- #region 拼音
- public static string ToChineseSpell(this string str)
- {
- return StringHelper.GetChineseSpell(str);
- }
- public static string ToChineseIndex(this string str)
- {
- return StringHelper.GetChineseIndex(str);
- }
- #endregion
- }
- }
|