/****************************** * 说明:字符串扩展 * 创建人:龚宇超 * 创建日期:2018-05-28 * 修改人: * 修改日期: * 修改备注: * 版本:1.0.0.0 ******************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Text.RegularExpressions; namespace Lskj.Util { public static class StringExtend { /// /// 说明:字符串转换为DataTable /// 创建人:龚宇超 /// 创建日期:2018-05-28 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The string data. /// The columns. /// DataTable. public static DataTable ToNewDataTable(this string stringData, string columns) { DataTable dtTable = new DataTable(); dtTable.Columns.Add(columns); stringData = stringData.Replace(" ", "").Replace(",,", ",").Trim(','); string[] confirmOper = stringData.Split(','); for (int i = 0; i < confirmOper.Length; i++) { DataRow row = dtTable.NewRow(); row[columns] = confirmOper[i]; dtTable.Rows.Add(row); } return dtTable; } /// /// 说明: /// 创建人:龚宇超 /// 创建日期:2017-10-23 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The s. /// The format. /// A that represents this instance. public static string ToStr(this object s, string format = "") { string result = string.Empty; try { if (string.IsNullOrWhiteSpace(format)) { result = s.ToString(); } else { result = string.Format("{0:" + format + "}", s); } } catch { } return result; } /// /// 说明:{key}字符串替换 /// 创建人:龚宇超 /// 创建日期:2018-05-28 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The key. /// The value. /// 是否忽略大小写 /// System.String. public static string Replace(this string str, string key, string value, bool ignoreCase) { //if (string.IsNullOrEmpty(str)) return str; //return Regex.IsMatch(str, key, RegexOptions.IgnoreCase) ? // System.Text.RegularExpressions.Regex.Replace(str, key, value, ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None) // : str; if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(key)) { return str; } var comparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; if (str.IndexOf(key, comparison) < 0) { return str; } return Regex.Replace( str, key, value ?? "", ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None); } /// /// 说明: /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The text. public static bool IsChinese(this string text) { for (int i = 0; i < text.Length; i++) { if ((int)text[i] > 127) return true; } return false; } } }