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
{
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 字符串扩展的更多相关文章
- es3中使用es6/7的字符串扩展
最近在看阮一峰的<ES6标准入门>,在字符串扩展一节中有提到几个新的扩展,觉得挺有意思,想在ES3里面使用,于是就有下面的兼容性写法. repeat 将一个字符串重复n次 String.p ...
- ES6字符串扩展
前面的话 字符串是编程中重要的数据类型,只有熟练掌握字符串操作才能更高效地开发程序.JS字符串的特性总是落后于其它语言,例如,直到 ES5 中字符串才获得了 trim() 方法.而 ES6 则继续添加 ...
- ES6中字符串扩展
ES6中字符串扩展 ① for...of 遍历字符串: 例如: for(let codePoint of 'string'){ console.log(codePoint) } 运行结果: ② in ...
- es6基础(4)--字符串扩展
//字符串扩展 { console.log('a','\u0061'); console.log('s','\u20BB7');//超过了0xffff console.log('s','\u{20BB ...
- 002-es6字符串扩展
1.字符串扩展 参考地址:http://es6.ruanyifeng.com/#docs/string 1.1.codePointAt() JavaScript 内部,字符以 UTF-16 的格式储存 ...
- 【ES6基础】字符串扩展
4.字符串扩展 (1)for...of循环遍历. let foo = [1,2,3,4,5,6] for(let i of foo){ console.log(i); } 结果: (2)include ...
- ES6之字符串扩展方法(常用)
es6这个String对象倒是扩展了不少方法,但是很多都是跟字符编码相关,个人选了几个感觉比较常用的方法: includes 搜索字符的神器 还记得我们之前如何判断某个字符串对象是否包含特地字符的吗? ...
- ES6之字符串扩展
ES6字符串新增的常用方法: 1. includes(): 字符串中是否包含某个字符或字符串, 包含的两个字符必须是相连的 let str = 'hello, world' str.includes( ...
- C# 一些常用的字符串扩展方法
以下可能是常用的.net扩展方法,记录下 EString.cs文件 /// <summary> /// 扩展字符串类 /// </summary> public static ...
随机推荐
- bzoj 1455: 罗马游戏 左偏树+并查集
1455: 罗马游戏 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 668 Solved: 247[Submit][Status] Descriptio ...
- linux 监控
http://www.iyunv.com/thread-50606-1-1.html http://segmentfault.com/a/1190000002537665 http://blog.cs ...
- 调试技巧 —— 如何利用windbg + dump + map分析程序异常
调试技巧 —— 如何利用windbg + dump + map分析程序异常 逗比汪星人2011-09-04上传 调试技巧 —— 如何利用windbg + dump + map分析程序异常 http ...
- 转载:浅谈Java多线程的同步问题【很好我就留下来,多分共享】
转载:http://www.cnblogs.com/phinecos/archive/2010/03/13/1684877.html#undefined 多线程的同步依靠的是对象锁机制,synchro ...
- Android之获得内存剩余大小与总大小
方法一: 如何查看android对应用的内存限制 每款手机对应用的限制都是不一样的,毕竟硬件不同,我们可以使用如下方式来查看单独的应用可使用的最大内存: 执行命令: adb shell getprop ...
- 【HDOJ】5288 OO’s Sequence
二分寻找对于指定pos的最左因数点和最右因数点. /* 5288 */ #include <iostream> #include <string> #include <m ...
- 产品设计中先熟练使用铅笔 不要依赖Axure
在互联网产品领域,Axure已成为产品经理.产品设计师以及交互设计师的必备工具,从某种程度讲,Axure帮助我们建立低保真模型,便于与用户的需求验证,也帮助我们构思交互细节,使前端和开发人员更容易理解 ...
- bzoj2434
利用了bzoj3172提到的性质,x串在y串中的出现的次数即为在fail树上以x结尾节点为根的子树中有多少个节点在y串上所以很明显我们要离线解决,我们先把询问按y分类存起来然后我们顺着操作顺序来,出现 ...
- POJ_3662_Telephone_Lines_(二分+最短路)
描述 http://poj.org/problem?id=3662 给一张图,要将1与n连起来.可以有k条边免费,其他边自费,付费的值为所有自费边中最大的值.求最小付费. Telephone Line ...
- [ZOJ 3623] Battle Ships
Battle Ships Time Limit: 2 Seconds Memory Limit: 65536 KB Battle Ships is a new game which is s ...