zoukankan      html  css  js  c++  java
  • 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;
            }
    
    
    
    
        }
    }
  • 相关阅读:
    [Linux]软件目录
    [Linux]查看Linux内核及发行版本
    [S7706]华为ACL
    [S7706]华为配置DHCP
    QML-密码管理器
    QML-AES加解密小工具
    LaTex中文article模板(支持代码、数学、TikZ)
    Memo-Tech
    VIM学习笔记
    CodeForces 674C Levels and Regions
  • 原文地址:https://www.cnblogs.com/chxl800/p/5752482.html
Copyright © 2011-2022 走看看