标签:color tchar [] obj using cas expr split new
using System; using System.Text.RegularExpressions; namespace Helpers { /// <summary> /// 包含常用字符串的转换 /// </summary> public static class StringEx { /// <summary> /// 判断是否为空 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static bool IsNullOrEmpty(this string obj) { return string.IsNullOrEmpty(obj); } /// <summary> /// 将字符串转换成整型数字 /// </summary> /// <param name="obj"></param> /// <param name="defaultValue">默认值</param> /// <returns></returns> public static int ToInt(this string obj, int defaultValue) { int result; if (!Int32.TryParse(obj, out result)) { result = defaultValue; } return result; } /// <summary> /// 转化为float 类型 /// </summary> /// <param name="obj"></param> /// <param name="defaultValue">默认值</param> /// <returns></returns> public static float ToFloat(this string obj, float defaultValue) { float result; if (!float.TryParse(obj, out result)) { result = defaultValue; } return result; } /// <summary> /// 转化为long 类型 /// </summary> /// <param name="obj"></param> /// <param name="defaultValue">默认值</param> /// <returns></returns> public static long ToLong(this string obj, long defaultValue) { long result; if (!Int64.TryParse(obj, out result)) { result = defaultValue; } return result; } /// <summary> /// 转化为Double 类型 /// </summary> /// <param name="obj"></param> /// <param name="defaultValue">默认值</param> /// <returns></returns> public static double ToDouble(this string obj, double defaultValue) { double result; if (!Double.TryParse(obj, out result)) { result = defaultValue; } return result; } /// <summary> /// 转化为Decimal 类型 /// </summary> /// <param name="obj"></param> /// <param name="defaultValue">默认值</param> /// <returns></returns> public static decimal ToDecimal(this string obj, decimal defaultValue) { decimal result; if (!decimal.TryParse(obj, System.Globalization.NumberStyles.Float, null, out result)) { result = defaultValue; } return result; } /// <summary> /// 转换为DateTime /// </summary> /// <param name="obj"></param> /// <param name="defaultValue">默认值</param> public static DateTime ToDateTime(this string obj, DateTime defaultValue) { DateTime result; if (!DateTime.TryParse(obj, out result)) { result = defaultValue; } return result; } /// <summary> /// 分割字符串 /// </summary> /// <param name="target">原字符</param> /// <param name="splitChars">分割符</param> /// <returns></returns> public static string[] Split(this string target, string splitChars) { return Regex.Split(target, splitChars, RegexOptions.IgnoreCase); } /// <summary> /// 字符串的格式化 /// </summary> /// <param name="format"></param> /// <param name="args"></param> /// <returns></returns> public static string FormatNewStr(this string format, params object[] args) { return string.Format(format, args); } } }
[No0000DD]C# StringEx 扩展字符串类 类封装
标签:color tchar [] obj using cas expr split new
原文地址:http://www.cnblogs.com/Chary/p/No0000DD.html