using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Service.Common
{
    public static class StringExt
    {
        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("\r", " ").Replace("\n", " ").Replace("<br />", " ").Replace("<br/>", " ").Replace("<br>", " ").Replace("\t", " ");
            }

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;
        }

}
}

C# StringExt 字符串扩展的更多相关文章

  1. es3中使用es6/7的字符串扩展

    最近在看阮一峰的<ES6标准入门>,在字符串扩展一节中有提到几个新的扩展,觉得挺有意思,想在ES3里面使用,于是就有下面的兼容性写法. repeat 将一个字符串重复n次 String.p ...

  2. ES6字符串扩展

    前面的话 字符串是编程中重要的数据类型,只有熟练掌握字符串操作才能更高效地开发程序.JS字符串的特性总是落后于其它语言,例如,直到 ES5 中字符串才获得了 trim() 方法.而 ES6 则继续添加 ...

  3. ES6中字符串扩展

    ES6中字符串扩展 ① for...of 遍历字符串: 例如: for(let codePoint of 'string'){ console.log(codePoint) } 运行结果: ②  in ...

  4. es6基础(4)--字符串扩展

    //字符串扩展 { console.log('a','\u0061'); console.log('s','\u20BB7');//超过了0xffff console.log('s','\u{20BB ...

  5. 002-es6字符串扩展

    1.字符串扩展 参考地址:http://es6.ruanyifeng.com/#docs/string 1.1.codePointAt() JavaScript 内部,字符以 UTF-16 的格式储存 ...

  6. 【ES6基础】字符串扩展

    4.字符串扩展 (1)for...of循环遍历. let foo = [1,2,3,4,5,6] for(let i of foo){ console.log(i); } 结果: (2)include ...

  7. ES6之字符串扩展方法(常用)

    es6这个String对象倒是扩展了不少方法,但是很多都是跟字符编码相关,个人选了几个感觉比较常用的方法: includes 搜索字符的神器 还记得我们之前如何判断某个字符串对象是否包含特地字符的吗? ...

  8. ES6之字符串扩展

    ES6字符串新增的常用方法: 1. includes(): 字符串中是否包含某个字符或字符串, 包含的两个字符必须是相连的 let str = 'hello, world' str.includes( ...

  9. C# 一些常用的字符串扩展方法

    以下可能是常用的.net扩展方法,记录下 EString.cs文件 /// <summary> /// 扩展字符串类 /// </summary> public static ...

随机推荐

  1. 【网络流24题】No.9 方格取数问题 (二分图点权最大独立集)

    [题意] 在一个有 m*n 个方格的棋盘中, 每个方格中有一个正整数. 现要从方格中取数, 使任意 2 个数所在方格没有公共边,且取出的数的总和最大.试设计一个满足要求的取数算法. 输入文件示例inp ...

  2. [收藏转贴]构建RESTful风格的WCF服务

    RESTful Wcf是一种基于Http协议的服务架构风格. 相较 WCF.WebService 使用 SOAP.WSDL.WS-* 而言,几乎所有的语言和网络平台都支持 HTTP 请求. RESTf ...

  3. SQL Server 字段状态判断语句

    selct newName=case when  条件 then '否' else '是' end from tableName

  4. js前台与后台数据交互-前台调后台

    转自:http://blog.csdn.net/wang379275614/article/details/17033981   网站是围绕数据库来编程的,以数据库中的数据为中心,通过后台来操作这些数 ...

  5. AjaxPro使用说明

    转自:http://www.cnblogs.com/lexus/archive/2007/11/29/977281.html 目录 AjaxPro使用说明    1 目录    2 修改历史纪录    ...

  6. 组合计数(polya计数):SGU 282 Isomorphism

    因为论文的题解写得太好了,直接贴. #include <iostream> #include <cstring> #include <cstdio> using n ...

  7. 整理:Google jQuery 引用地址大全和方法(转)

    什么是google的js托管? 说的明白点,跟我们以往做法一样,只不过这时候的引用的js库是放在google服务器上的. 比如引用jquery,则使用路径  http://ajax.googleapi ...

  8. Ajax初步实现页面局部内容更替

    类似于QQ邮箱的那种局部页面跳转,单页应用常用到,目前很多网页都是这种,但是弊端就是一次加载过多资源,首次加载卡出翔啊

  9. HDOJ/HDU 1328 IBM Minus One(水题一个,试试手)

    Problem Description You may have heard of the book '2001 - A Space Odyssey' by Arthur C. Clarke, or ...

  10. How to compile pycrypto 2.4.1 (python 3.2.2 for Windows 7 x64)

    How to compile pycrypto 2.4.1 (python 3.2.2 for Windows 7 x64) Nov 10 Posted by alesk This note is a ...