• C# StringExt 字符串扩展


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Service.Common
    {
        public static class StringExt
        {

             /// <summary>
             /// 把集合拼接成用分隔符分割的字符串
             /// </summary>
             /// <param name="source"></param>
             /// <param name="separator"></param>
             /// <returns></returns>
             public static string StringJoin<T>(this IEnumerable<T> source, string separator)
             {
                  return string.Join(separator, source);
             }

            public static string JoinToString<T>(this IEnumerable<T> collection, string split)
            {
                if (collection == null || !collection.Any()) return string.Empty;
    
                StringBuilder sb = new StringBuilder();
                foreach (T t in collection)
                {
                    sb.Append(t).Append(split);
                }
                if (sb.Length > 0)
                {
                    sb.Remove(sb.Length - split.Length, split.Length);
                }
    
                return sb.ToString();
            }
    
            public static DateTime ToDateTime(this string value, DateTime defaultValue)
            {
                DateTime temp;
                if (DateTime.TryParse(value, out temp))
                {
                    return temp;
                }
                else
                {
                    return defaultValue;
                }
            }
    
            public static int ToInt32(this string value, int defaultValue)
            {
                int temp;
                if (int.TryParse(value, out temp))
                {
                    return temp;
                }
                else
                {
                    return defaultValue;
                }
            }
    
            public static long ToInt64(this string value, long defaultValue)
            {
                long temp;
                if (long.TryParse(value, out temp))
                {
                    return temp;
                }
                else
                {
                    return defaultValue;
                }
            }
    
            public static decimal ToDecimal(this string value, decimal defaultValue)
            {
                decimal temp;
                if (decimal.TryParse(value, out temp))
                {
                    return temp;
                }
                else
                {
                    return defaultValue;
                }
            }
    
            public static double ToDouble(this string value, double defaultValue)
            {
                double temp;
                if (double.TryParse(value, out temp))
                {
                    return temp;
                }
                else
                {
                    return defaultValue;
                }
            }
    
            public static float ToSingle(this string value, float defaultValue)
            {
                float temp;
                if (float.TryParse(value, out temp))
                {
                    return temp;
                }
                else
                {
                    return defaultValue;
                }
            }
    
            public static DateTime? ToDateTimeNullable(this string value)
            {
                DateTime temp;
                if (DateTime.TryParse(value, out temp))
                {
                    return temp;
                }
                else
                {
                    return null;
                }
            }
    
            public static int? ToInt32Nullable(this string value)
            {
                int temp;
                if (int.TryParse(value, out temp))
                {
                    return temp;
                }
                else
                {
                    return null;
                }
            }
    
            public static long? ToInt64Nullable(this string value)
            {
                long temp;
                if (long.TryParse(value, out temp))
                {
                    return temp;
                }
                else
                {
                    return null;
                }
            }
    
            public static decimal? ToDecimalNullable(this string value)
            {
                decimal temp;
                if (decimal.TryParse(value, out temp))
                {
                    return temp;
                }
                else
                {
                    return null;
                }
            }
    
            public static double? ToDoubleNullable(this string value)
            {
                double temp;
                if (double.TryParse(value, out temp))
                {
                    return temp;
                }
                else
                {
                    return null;
                }
            }
    
            public static float? ToSingleNullable(this string value)
            {
                float temp;
                if (float.TryParse(value, out temp))
                {
                    return temp;
                }
                else
                {
                    return null;
                }
            }
    
     
    
           public static T? Parse<T>(this string value, Func<string, T> parseFunc)
                where T : struct
            {
                if (string.IsNullOrEmpty(value))
                {
                    return null;
                }
                else
                {
                    return parseFunc(value.Trim());
                }
            }
    
            public static int TryParseInt(this string value)
            {
                int temp;
                if (int.TryParse(value, out temp))
                {
                    return temp;
                }
                else
                {
                    return 0;
                }
            }
    
            /// <summary>
            /// 类型转换
            /// </summary> 
            public static T Value<T>(this object value)
            {
                if (value == null)
                {
                    return default(T);
                } 
                if (value is T)
                    return (T)value;
                else if (value == DBNull.Value)
                {
                    if (typeof(T) == typeof(DateTime))
                    {
                        object o = new DateTime(1900, 1, 1);
                        return (T)o;
                    }
                    else
                        return default(T);
                }
                else if (value.ToString().ToLower() == "null")
                    return default(T);            
                else
                    return (T)Convert.ChangeType(value, typeof(T)); 
            }
    
            public static T Value<T>(this object value, Func<object, T> funcConvert)
            {
                if (value == null)
                {
                    return default(T);
                }
    
                string s = value.ToString();
                if (string.IsNullOrEmpty(s) || s.ToLower() == "null")
                {
                    return default(T);
                }
    
                return funcConvert(value);
            }
    
            public static object Value(this object value)
            {
                return Value<object>(value);
            }
    
            public static string DecimalToString(this object value, string format)
            {
                if (value == null) return string.Empty;
                if (value is string)
                {
                    if (value == string.Empty) return string.Empty;
                    return Convert.ToDecimal(value).ToString(format);
                }
                else if (value is decimal)
                {
                    return Convert.ToDecimal(value).ToString(format);
                }
                else
                {
                    return value.ToString();
                }
            }
    
            public static string ValueToString(this object value)
            {
                return ValueToString(value, false);
            }
    
            public static string ValueToString(this object value, bool removeLines)
            {
                if (value == null)
                {
                    return string.Empty;
                }
    
                string s = value.ToString();
                if (string.IsNullOrEmpty(s) || s.ToLower() == "null")
                {
                    return string.Empty;
                }
    
                if (removeLines)
                {
                    s = s.Replace("
    ", " ").Replace("
    ", " ").Replace("<br />", " ").Replace("<br/>", " ").Replace("<br>", " ").Replace("	", " ");
                }
    
                return s;
            }
    
            public static object IsNull(this object value, object replaceValue)
            {
                if (value == null)
                {
                    return replaceValue;
                }
                else
                {
                    return value;
                }
            }
    
            public static T IsNull<T>(this T value, T replaceValue)
            {
                if (value.Equals(default(T)))
                {
                    return replaceValue;
                }
                else
                {
                    return value;
                }
            }
    
            public static string IsNull(this string value, string replaceValue)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return replaceValue;
                }
                else
                {
                    return value;
                }
            }
    
            /// <summary>
            /// 去除非法的文件字符
            /// </summary>
            /// <param name="input"></param>
            /// <returns></returns>
            public static string RemoveInvalidFileChars(this string input)
            {
                char[] invalidChars = Path.GetInvalidFileNameChars();
                foreach (char c in invalidChars)
                {
                    input = input.Replace(c.ToString(), "");
                }
                return input;
            }
    
    
    
    
        }
    }
  • 相关阅读:
    解决win7和win8的64位系统安装NetAdvantage时总是提示%SystemDriver%inetpub\wwwroot错误的方法
    如何使VS2008 调试网站的根目录和IIS调试的一致?
    设置网站默认目录
    asp.net网站未能加载文件或程序集或它的某一个依赖项。拒绝访问
    HTML5 :Canvas之基本用法
    HTML5 Canvas之图片引用
    python虚拟环境的介绍和创建
    基于jTemplates、ascx协同工作的酷UI模板技术
    我的.Subtext二次开发之路系列:无限层次分类
    我的.Subtext二次开发之路系列:引言
  • 原文地址:https://www.cnblogs.com/chxl800/p/5752482.html
Copyright © 2020-2023  润新知