BaseExtend

using Intesim.Global;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace xxxx.Common
{
    public static class BaseExtend
    {

        #region string类
        public static bool IsEmpty(this string s)
        {
            return string.IsNullOrEmpty(s);
        }

        public static bool IsEmptyTrim(this string s)
        {

            if (s != null)
            {
                return string.IsNullOrEmpty(s);
            }
            else
            {
                return true;
            }


        }

        public static bool IsNotEmptyTrim(this string s)
        {


            if (s!=null)
            {
                return !string.IsNullOrEmpty(s.Trim());
            }
            else
            {
                return false;
            }

        }

        public static string FormatWith(this string s, params object[] args)
        {
            return string.Format(s, args);
        }

        public static bool ToBool(this string s, bool defaultVal = false)
        {
            bool t=false;
            bool b = bool.TryParse(s, out  t);

            return b ? t : defaultVal;


        }

        public static int ToInt(this string s, int defaultVal = 0)
        {
            int t=0;
            bool b = int.TryParse(s, out t);
            return b ? t : defaultVal;
        }

        public static int? ToIntNull(this string s, int? defaultVal = null)
        {
            int t=0;
            bool b = int.TryParse(s, out t);
            //return b ? t : null;
            if (b)
            {
                return t;
            }
            else
            {
                return defaultVal;
            }
        }
        public static float ToFloat(this string s, float defaultVal = 0)
        {
            float t = 0;
            bool b = float.TryParse(s, out t);
            return b ? t : defaultVal;
        }

        public static double ToDouble(this string s, double defaultVal = 0)
        {
            double t = 0;
            bool b = double.TryParse(s, out t);
            return b ? t : defaultVal;
        }

        public static DateTime? ToDataTime(this string s, DateTime? defaultVal = null)
        {
            DateTime t = DateTime.Now;
            bool b = DateTime.TryParse(s, out t);
            return b ? (DateTime?)t : defaultVal;
        }

        public static bool IsMatch(this string s, string pattern)
        {
            if (s == null) return false;
            else return Regex.IsMatch(s, pattern);
        }

        public static string Match(this string s, string pattern)
        {
            if (s == null) return "";
            return Regex.Match(s, pattern).Value;
        }

        //public static void Test3()
        //{
        //    bool b = "12345".IsMatch(@"\d+");
        //    string s = "ldp615".Match("[a-zA-Z]+");
        //}

        /// <summary>
        /// 转换为字符串
        /// </summary>
        /// <param name="obj">当对象为空,返回string.Empty</param>
        /// <returns></returns>
        public static string ToString2(this object obj)
        {
            if (obj == null) return string.Empty;
            return obj.ToString().Trim();
        }

        /// <summary>
        /// 首字母大写包括分隔符 sim_org => Sim_Org
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string FirstAnyToUpper(this string s)
        {
            return System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(s);
        }

        /// <summary>
        /// 首字母大写 sim_org => Sim_org
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string FirstToUpper(this string s)
        {
            if(string.IsNullOrWhiteSpace(s))
            {
                return string.Empty;
            }

            char[] str = s.ToCharArray();
            char c = str[0];

            if( c >= 'a' && c <= 'z')
            {
                c = (char)(c & ~0x20); //32 => 00100000 => 11011111 & 01100001 => 01000001
            }

            str[0] = c;

            return new string(str);
        }

        /// <summary>
        /// 首字母小写 Sim_org => sim_org
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string FirstToLower(this string str)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                return string.Empty;
            }
            str = str.First().ToString().ToLower() + str.Substring(1);
            return str;
        }

        /// <summary>
        /// 将大写字母转为下划线加小写字母
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string UpperTo_Lower(this string s)
        {
            if (string.IsNullOrWhiteSpace(s))
            {
                return string.Empty;
            }
            Regex reg = new Regex(@"[A-Z]");
            MatchCollection matchRet = reg.Matches(s);
            foreach (Match item in matchRet)
            {
               s= s.Replace(item.Groups[0].Value, '_'+ item.Groups[0].Value.ToLower());
            }
            return s;
        }

        public static string _ToUpper(this string s)
        {
            if (string.IsNullOrWhiteSpace(s))
            {
                return string.Empty;
            }
            string[] arrStr = Split(s, new string[] {"_"});
            string rS = string.Empty;
            if (arrStr.Length>1)
            {
                foreach (var s1 in arrStr)
                {
                    rS += s1.FirstToUpper();
                }
                return rS.FirstToLower();
            }
            else
            {
                return arrStr[0];
            }
            
            
        }

        /// <summary>
        /// 分解字符串
        /// </summary>
        /// <param name="s"></param>
        /// <param name="splits"></param>
        /// <returns></returns>
        public static string[] Split(this string s, string splits)
        {
            return s.Split(new string[]{splits}, StringSplitOptions.RemoveEmptyEntries);
        }

        /// <summary>
        /// 分解字符串
        /// </summary>
        /// <param name="s"></param>
        /// <param name="splits"></param>
        /// <returns></returns>
        public static string[] Split(this string s, string[] splits)
        {
            return s.Split(splits, StringSplitOptions.RemoveEmptyEntries);
        }
        #endregion



        public static bool HasValue(this object thisValue)
        {
            if (thisValue == null || thisValue == DBNull.Value) return false;
            return thisValue.ToString() != "";
        }

        public static bool HasValue(this IEnumerable<object> thisValue)
        {
            if (thisValue == null || thisValue.Count() == 0) return false;
            return true;
        }

        public static bool IsValuable(this IEnumerable<KeyValuePair<string, string>> thisValue)
        {
            if (thisValue == null || thisValue.Count() == 0) return false;
            return true;
        }

        /// <summary>
        ///object转换为可空类型 by - ty.zheng
        /// </summary>
        /// <param name="thisValue"></param>
        /// <param name="convertsionType"></param>
        /// <returns></returns>
        public static object ChangeType(this object thisValue , Type convertsionType)
        {
            if(convertsionType.IsGenericType &&
                convertsionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
            {
                if(!thisValue.HasValue() || thisValue.ToString().Length == 0)
                {
                    return null;
                }

                NullableConverter nullableConverter = new NullableConverter(convertsionType);

                convertsionType = nullableConverter.UnderlyingType;
            }
            return Convert.ChangeType(thisValue, convertsionType);
        }

        #region DateTime类
        public static string ToShortStr(this DateTime s)
        {
            return s.ToString(GloConst.DEF_DATE_FORMAT);
        }

        #endregion

        #region 泛型类

        /// <summary>
        /// 通过属性名获取值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static object GetValueByPropertyName<T>(this T entity, string propertyName)
        {
            if (entity.HasValue() && !propertyName.IsEmpty())
            {
                return entity.GetType().GetProperty(propertyName).GetValue(entity, null);
            }
            return null;
        }

        #endregion
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值