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. SPRING IN ACTION 第4版笔记-第一章-005-Bean的生命周期

    一. 1. As you can see, a bean factory performs several setup steps before a bean is ready touse. Let’ ...

  2. XtraForm默认皮肤的显示

    1.新建一个XtraForm窗体 2.运行显示这个窗体,会发现没有任何变化,显示的还是winform的样式 3.在Program.cs文件的Main函数中添加 DevExpress.Skins.Ski ...

  3. POJ_3280_Cheapest_Palindrome_(动态规划)

    描述 http://poj.org/problem?id=3280 n 种小写字母构成长度为 m 的串,现在要通过增删字母使串回文,给出每种字母增和删的费用,求最小花费. Cheapest Palin ...

  4. ☀【Grunt】package.json, Gruntfile.js, npm install, grunt

    npm install --registry http://registry.npm.taobao.org/ 切换源 Grunt.js 在前端项目中的实战http://beiyuu.com/grunt ...

  5. 在Windows Azure公有云环境部署企业应用

    作者 王枫 发布于 2014年4月5日 企业内部应用转换为在线服务 Windows Azure已经成为众多IT服务提供商们热议的话题,其中,有的认为只有提供互连网用户服务的应用才适合放在公有云环境内运 ...

  6. 新浪微博授权时出现"关注 *** 的微博"

    基本设置完成后, 保证scope是 "all" 或 至少 包含 "follow_app_official_microblog". 测试时, 保证你的测试账号没有 ...

  7. ssh 登录出现的几种错误以及解决办法

    首先.确保server端的ssh服务是开的(service shhd start) 然后在client端输入: ssh usrname@serverip (远程登录) scp filename usr ...

  8. HDU-2523 SORT AGAIN

    http://acm.hdu.edu.cn/showproblem.php?pid=2523 学习哈希和注意i++,后要--i: SORT AGAIN Time Limit: 2000/1000 MS ...

  9. MVC和Web API 过滤器Filter [转]

    ASP.NET MVC 支持以下类型的操作筛选器: ·        授权筛选器.这些筛选器用于实现IAuthorizationFilter和做出关于是否执行操作方法(如执行身份验证或验证请求的属性) ...

  10. JavaScript---网络编程(6)-Dom和Bom模型概念讲解(节点获取,window对象)

    本节讲Dom和Bom模型概念讲解(节点获取,window对象). out.js: 写了2个输出到页面的函数. function println(param){ document.write(param ...