using System.ComponentModel; using System.Reflection; using System.Text; namespace Tiobon.Core.Common.Helper; public class StringHelper { /// /// 根据分隔符返回前n条数据 /// /// 数据内容 /// 分隔符 /// 前n条 /// 是否倒序(默认false) /// public static List GetTopDataBySeparator(string content, string separator, int top, bool isDesc = false) { if (string.IsNullOrEmpty(content)) { return new List() { }; } if (string.IsNullOrEmpty(separator)) { throw new ArgumentException("message", nameof(separator)); } var dataArray = content.Split(separator).Where(d => !string.IsNullOrEmpty(d)).ToArray(); if (isDesc) { Array.Reverse(dataArray); } if (top > 0) { dataArray = dataArray.Take(top).ToArray(); } return dataArray.ToList(); } /// /// 根据字段拼接get参数 /// /// /// public static string GetPars(Dictionary dic) { StringBuilder sb = new StringBuilder(); string urlPars = null; bool isEnter = false; foreach (var item in dic) { sb.Append($"{(isEnter ? "&" : "")}{item.Key}={item.Value}"); isEnter = true; } urlPars = sb.ToString(); return urlPars; } /// /// 根据字段拼接get参数 /// /// /// public static string GetPars(Dictionary dic) { StringBuilder sb = new StringBuilder(); string urlPars = null; bool isEnter = false; foreach (var item in dic) { sb.Append($"{(isEnter ? "&" : "")}{item.Key}={item.Value}"); isEnter = true; } urlPars = sb.ToString(); return urlPars; } /// /// 获取一个GUID /// /// 格式-默认为N /// public static string GetGUID(string format = "N") { return Guid.NewGuid().ToString(format); } /// /// 根据GUID获取19位的唯一数字序列 /// /// public static long GetGuidToLongID() { byte[] buffer = Guid.NewGuid().ToByteArray(); return BitConverter.ToInt64(buffer, 0); } /// /// 获取字符串最后X行 /// /// /// /// public static string GetCusLine(string resourceStr, int length) { string[] arrStr = resourceStr.Split("\r\n"); return string.Join("", (from q in arrStr select q).Skip(arrStr.Length - length + 1).Take(length).ToArray()); } #region 求系统唯一字符串 /// /// 求系统唯一字符串,常用于ROW_ID值。 /// /// 字符串 public static string GetSysID() { string sid = string.Empty; byte[] buffer = Guid.NewGuid().ToByteArray(); sid = DateTime.Now.ToString("yyMMddHHmmss") + BitConverter.ToInt64(buffer, 0).ToString(); return sid; } #endregion #region 格式化数字字符 /// /// 格式化数字字符,如传入1.24500,返回1.245 /// /// /// public static string TrimDecimalString(string value) { try { string result = string.Empty; if (!string.IsNullOrEmpty(value)) { Decimal tmp = Decimal.Parse(value); result = string.Format("{0:#0.##########}", tmp); } return result; } catch (Exception) { throw; } } public static string TrimDecimalString(decimal value) { return TrimDecimalString(value.ToString()); } public static string TrimDecimalString(decimal? value) { if (value is null) return ""; return TrimDecimalString(value.ToString()); } /// /// 格式化数字字符,并保留指定的小数位 /// /// 需要处理的值 /// 保留小数点后位数 /// public static string TrimDecimalString(string value, int reservedDigit) { try { string result = string.Empty; if (!string.IsNullOrEmpty(value)) { Decimal tmp = Decimal.Parse(value); if (reservedDigit == -1) result = string.Format("{0:#0.##########}", tmp); else { result = String.Format("{0:N" + reservedDigit.ToString() + "}", tmp); result = result.Replace(",", ""); } } return result; } catch (Exception) { throw; } } /// /// 格式化数字字符,并保留指定的小数位 /// /// 需要处理的值 /// 保留小数点后位数,-1时只会去除小数点后最后几位的0 /// public static string TrimDecimalString(object value, int reservedDigit) { try { string result = string.Empty; if (!string.IsNullOrEmpty(Convert.ToString(value))) { Decimal tmp = Decimal.Parse(Convert.ToString(value)); if (reservedDigit == -1) result = string.Format("{0:#0.##########}", tmp); else { result = String.Format("{0:N" + reservedDigit.ToString() + "}", tmp); result = result.Replace(",", ""); } } return result; } catch (Exception) { throw; } } /// /// 格式化数字字符,并保留指定的小数位 /// /// 需要处理的值 /// 保留小数点后位数,-1时只会去除小数点后最后几位的0 /// public static decimal TrimDecimal(object value, int reservedDigit) { try { string result = string.Empty; if (!string.IsNullOrEmpty(Convert.ToString(value))) { Decimal tmp = Decimal.Parse(Convert.ToString(value)); if (reservedDigit == -1) result = string.Format("{0:#0.##########}", tmp); else { result = String.Format("{0:N" + reservedDigit.ToString() + "}", tmp); result = result.Replace(",", ""); } } return Convert.ToDecimal(result); } catch (Exception) { throw; } } #endregion #region 获取字段描述 /// /// 对象字段描述 /// private static Dictionary> m_FieldDesc = new Dictionary>(); /// /// 获取字段的描述(描述 - 列名) /// /// /// public static Dictionary GetFieldDesc() { var type = typeof(T).ToString(); lock (m_FieldDesc) { if (m_FieldDesc.ContainsKey(type)) return m_FieldDesc[type]; } Dictionary dic = new Dictionary(); try { PropertyInfo[] peroperties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); if (peroperties != null) { foreach (PropertyInfo property in peroperties) { object[] objs = property.GetCustomAttributes(typeof(DescriptionAttribute), true); if (objs.Length > 0) { var desc = ((DescriptionAttribute)objs[0]).Description.Trim(); if (!dic.ContainsKey(desc)) { dic.Add(desc, property.Name); } else { dic[desc] = property.Name; } } } } } catch //(Exception ex) { } lock (m_FieldDesc) { if (!m_FieldDesc.ContainsKey(type)) m_FieldDesc.Add(type, dic); } return dic; } #endregion }