首页 > 代码库 > [No0000DD]C# StringEx 扩展字符串类 类封装
[No0000DD]C# StringEx 扩展字符串类 类封装
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 扩展字符串类 类封装
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。