• [No0000DD]C# StringEx 扩展字符串类 类封装


    using System;
    using System.Text.RegularExpressions;
    
    namespace Helpers
    {
        /// <summary>
        /// 包含常用字符串的转换
        /// </summary>
        public static class StringEx
        {
            /// <summary>
            /// 判断是否为空
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static bool IsNullOrEmpty(this string obj)
            {
                return string.IsNullOrEmpty(obj);
            }
    
            /// <summary>
            /// 将字符串转换成整型数字
            /// </summary>
            /// <param name="obj"></param>
            /// <param name="defaultValue">默认值</param>
            /// <returns></returns>
            public static int ToInt(this string obj, int defaultValue)
            {
                int result;
                if (!Int32.TryParse(obj, out result))
                {
                    result = defaultValue;
                }
                return result;
            }
    
            /// <summary>
            /// 转化为float 类型
            /// </summary>
            /// <param name="obj"></param>
            /// <param name="defaultValue">默认值</param>
            /// <returns></returns>
            public static float ToFloat(this string obj, float defaultValue)
            {
                float result;
                if (!float.TryParse(obj, out result))
                {
                    result = defaultValue;
                }
                return result;
            }
    
            /// <summary>
            /// 转化为long 类型
            /// </summary>
            /// <param name="obj"></param>
            /// <param name="defaultValue">默认值</param>
            /// <returns></returns>
            public static long ToLong(this string obj, long defaultValue)
            {
                long result;
                if (!Int64.TryParse(obj, out result))
                {
                    result = defaultValue;
                }
                return result;
            }
    
            /// <summary>
            /// 转化为Double 类型
            /// </summary>
            /// <param name="obj"></param>
            /// <param name="defaultValue">默认值</param>
            /// <returns></returns>
            public static double ToDouble(this string obj, double defaultValue)
            {
                double result;
                if (!Double.TryParse(obj, out result))
                {
                    result = defaultValue;
                }
                return result;
            }
            /// <summary>
            /// 转化为Decimal 类型
            /// </summary>
            /// <param name="obj"></param>
            /// <param name="defaultValue">默认值</param>
            /// <returns></returns>
            public static decimal ToDecimal(this string obj, decimal defaultValue)
            {
                decimal result;
                if (!decimal.TryParse(obj, System.Globalization.NumberStyles.Float, null, out result))
                {
                    result = defaultValue;
                }
                return result;
            }
    
            /// <summary>
            /// 转换为DateTime
            /// </summary>
            /// <param name="obj"></param>
            /// <param name="defaultValue">默认值</param>
            public static DateTime ToDateTime(this string obj, DateTime defaultValue)
            {
                DateTime result;
                if (!DateTime.TryParse(obj, out result))
                {
                    result = defaultValue;
                }
                return result;
            }
    
            /// <summary>
            /// 分割字符串
            /// </summary>
            /// <param name="target">原字符</param>
            /// <param name="splitChars">分割符</param>
            /// <returns></returns>
            public static string[] Split(this string target, string splitChars)
            {
                return Regex.Split(target, splitChars, RegexOptions.IgnoreCase);
            }
    
            /// <summary>
            /// 字符串的格式化
            /// </summary>
            /// <param name="format"></param>
            /// <param name="args"></param>
            /// <returns></returns>
            public static string FormatNewStr(this string format, params object[] args)
            {
                return string.Format(format, args);
            }
        }
    }
  • 相关阅读:
    zend studio中安装Emmet插件后迅速编写html的方法
    ZendStudio 代码调试
    PHP中循环结构之foreach循环语句
    【题解】物流运输 [ZJ2006] [P1772] [BZOJ1003]
    【题解】最大 M 子段和 Max Sum Plus Plus [Hdu1024] [51nod1052]
    【题解】最长递增路径 [51nod1274]
    【题解】与查询 [51nod1406]
    【题解】选数字 [51nod1354]
    【题解】逆序排列 [51nod1020]
    【题解】整数划分 [51nod1201] 整数划分 V2 [51nod1259]
  • 原文地址:https://www.cnblogs.com/Chary/p/No0000DD.html
Copyright © 2020-2023  润新知