| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- using IwbZero.IwbBase;
- using IwbZero.IwbDataQuery;
- namespace IwbZero.ToolCommon.StringModel
- {
- public static class StringHelper
- {
- #region STRING
- /// <summary>
- /// 检查空字符串
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool IsNotEmpty(this string str)
- {
- return !IsEmpty(str);
- }
- /// <summary>
- /// 检查空字符串
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool IsEmpty(this string str)
- {
- return string.IsNullOrEmpty(str);
- }
- /// <summary>
- /// 大写并移除空白字符串
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static string UAndT(this string str)
- {
- if (str == null)
- str = "";
- return str.ToUpper().Trim();
- }
- /// <summary>
- /// 大写并移除空白字符串
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static string UAndT(this object obj)
- {
- return UAndT(obj.ToString());
- }
- /// <summary>
- /// 检查是否以某字符串开头,不是加上此字符串
- /// </summary>
- /// <param name="str"></param>
- /// <param name="startStr"></param>
- /// <returns></returns>
- public static string Sw(this string str, string startStr)
- {
- return str.StartsWith(startStr) ? str : $"{startStr}{str}";
- }
- /// <summary>
- /// 检查是否以某字符串结尾,不是加上此字符串
- /// </summary>
- /// <param name="str"></param>
- /// <param name="endStr"></param>
- /// <returns></returns>
- public static string Ew(this string str, string endStr)
- {
- return str.EndsWith(endStr) ? str : $"{str}{endStr}";
- }
- /// <summary>
- /// 前补全
- /// </summary>
- /// <param name="i"></param>
- /// <param name="num"></param>
- /// <param name="charStr"></param>
- /// <returns></returns>
- public static string LeftPad(this int i, int num, string charStr = "0")
- {
- return i.ToString().PadLeft(num, Convert.ToChar(charStr));
- }
- /// <summary>
- /// 后补全
- /// </summary>
- /// <param name="i"></param>
- /// <param name="num"></param>
- /// <param name="charStr"></param>
- /// <returns></returns>
- public static string RightPad(this int i, int num, string charStr = "0")
- {
- return i.ToString().PadRight(num, Convert.ToChar(charStr));
- }
- /// <summary>
- /// 前补全
- /// </summary>
- /// <param name="s"></param>
- /// <param name="num"></param>
- /// <param name="charStr"></param>
- /// <returns></returns>
- public static string LeftPad(this string s, int num, string charStr = "0")
- {
- return s.PadLeft(num, Convert.ToChar(charStr));
- }
- /// <summary>
- /// 后补全
- /// </summary>
- /// <param name="s"></param>
- /// <param name="num"></param>
- /// <param name="charStr"></param>
- /// <returns></returns>
- public static string RightPad(this string s, int num, string charStr = "0")
- {
- return s.PadRight(num, Convert.ToChar(charStr));
- }
- /// <summary>
- /// 复制
- /// </summary>
- /// <param name="str"></param>
- /// <param name="count">复制次数</param>
- /// <returns></returns>
- public static string Replicate(this string str, int count)
- {
- string str2 = "";
- for (int index = 0; index < count; ++index)
- str2 += str;
- return str2;
- }
- /// <summary>
- /// 末尾移除匹配
- /// </summary>
- /// <param name="source"></param>
- /// <param name="removeStr"></param>
- /// <returns></returns>
- public static string TrimEnd(string source, string removeStr)
- {
- char[] charArray = removeStr.ToCharArray();
- return source.TrimEnd(charArray);
- }
- /// <summary>
- /// 截取字符串中开始和结束字符串中间的字符串
- /// </summary>
- /// <param name="source">源字符串</param>
- /// <param name="startStr">开始字符串</param>
- /// <param name="endStr">结束字符串</param>
- /// <returns>中间字符串</returns>
- public static string SubstringSingle(this string source, string startStr, string endStr)
- {
- Regex rg = new Regex("(?<=(" + startStr + "))[.\\s\\S]*?(?=(" + endStr + "))", RegexOptions.Multiline | RegexOptions.Singleline);
- return rg.Match(source).Value;
- }
- /// <summary>
- /// (批量)截取字符串中开始和结束字符串中间的字符串
- /// </summary>
- /// <param name="source">源字符串</param>
- /// <param name="startStr">开始字符串</param>
- /// <param name="endStr">结束字符串</param>
- /// <returns>中间字符串</returns>
- public static List<string> SubstringMultiple(this string source, string startStr, string endStr)
- {
- Regex rg = new Regex("(?<=(" + startStr + "))[.\\s\\S]*?(?=(" + endStr + "))", RegexOptions.Multiline | RegexOptions.Singleline);
- MatchCollection matches = rg.Matches(source);
- List<string> resList = new List<string>();
- foreach (Match item in matches)
- resList.Add(item.Value);
- return resList;
- }
- private static Random _random;
- public static string GetRandom()
- {
- if (_random == null)
- {
- _random = new Random(0x989680);
- }
- return "R" + _random.Next(0x989680, 0x5f5e0ff);
- }
- public static int GetRandomSeed()
- {
- byte[] bytes = new byte[4];
- var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
- rng.GetBytes(bytes);
- return BitConverter.ToInt32(bytes, 0);
- }
-
- public static string FormatXml2Code(this string str)
- {
- return str.Replace(";amp;", "&").Replace(";lt;", "<").Replace(";gt;", ">");
- }
- public static string FormatCode2Xml(this string str)
- {
- return str.Replace("&",";amp;" ).Replace("<", ";lt;").Replace(">", ";gt;");
- }
- public static string AddStr(this string source, object toBeAppended)
- {
- source = source.Trim();
- if (source == "")
- {
- return GetDelimitedStr(toBeAppended);
- }
- if ((source[0] == '[') && (source[source.Length - 1] == ']'))
- {
- return (source + $",[{toBeAppended.ToString().Trim()}]");
- }
- return JoinString(source, toBeAppended);
- }
- public static string JoinString(this object first, object second)
- {
- return $"[{first.ToString().Trim()}],[{second.ToString().Trim()}]";
- }
- public static string JoinString(this object first, object[] second)
- {
- var str = "";
- foreach (var o in second)
- {
- str += $",[{o.ToString().Trim()}]";
- }
- return $"[{first.ToString().Trim()}]{str}";
- }
- public static string JoinString(this object[] second)
- {
- var str = "";
- foreach (var o in second)
- {
- str += (str == "" ? "" : ",") + $"[{o.ToString().Trim()}]";
- }
- return str;
- }
- public static string JoinString(this object first, object second, object poThird)
- {
- return $"[{first.ToString().Trim()}],[{second.ToString().Trim()}], [{poThird.ToString().Trim()}]";
- }
- public static string JoinString(this object first, object second, object poThird, object poFourth)
- {
- return
- $"[{first.ToString().Trim()}],[{second.ToString().Trim()}], [{poThird.ToString().Trim()}], [{poFourth.ToString().Trim()}]";
- }
- public static string AppendToStr(this string source, object toBeAppended)
- {
- string delimitedStr = GetDelimitedStr(toBeAppended);
- source = source.Trim();
- if (source != "")
- {
- delimitedStr = source + "," + delimitedStr;
- }
- return delimitedStr;
- }
- #endregion STRING
- #region INT,DECIMAL,BOOL
- /// <summary>
- /// 验证int,并返回
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static int ValI(this string str)
- {
- return (int)ValD(str);
- }
- /// <summary>
- /// 验证decimal,并返回
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static decimal ValD(this string str)
- {
- string str1 = "";
- foreach (var ch in str)
- {
- if (ch != ' ' && ch != ',')
- {
- if (ch >= '0' && ch <= '9')
- str1 += ch.ToString();
- else if ((ch == '+' || ch == '-') && str1 == "")
- str1 += ch.ToString();
- else if (ch == '.' && str1.IndexOf(ch) < 0)
- str1 += ch.ToString();
- else
- break;
- }
- }
- if (str1 == "")
- str1 = "0";
- else if (str1 == "-")
- str1 = "-0";
- return Convert.ToDecimal(str1);
- }
- /// <summary>
- /// int转bool (true,yes,y,>1)
- /// </summary>
- /// <param name="i"></param>
- /// <returns></returns>
- public static bool ValB(this int i)
- {
- var str = i > 0 ? "1" : "0";
- return StrToBool(str);
- }
- /// <summary>
- /// 字符串转bool (true,yes,y,1)
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool ValB(this string str)
- {
- return StrToBool(str);
- }
- /// <summary>
- /// 字符串转bool (true,yes,y,1)
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool StrToBool(this string str)
- {
- bool flag = false;
- str = UAndT(str);
- if (str == "TRUE" || str == "YES" || str == "Y" || str == "1")
- flag = true;
- return flag;
- }
- #endregion INT,DECIMAL,BOOL
- #region Array
- /// <summary>
- /// 字符转数组
- /// </summary>
- /// <param name="str"></param>
- /// <param name="separator">分隔符默认:,</param>
- /// <returns></returns>
- public static string[] StrToArray(this string str, string separator = ",")
- {
- return (string[])StrToArrayEx(str, separator);
- }
- /// <summary>
- /// 字符转数组
- /// </summary>
- /// <param name="str"></param>
- /// <param name="separator">分隔符默认:,</param>
- /// <returns></returns>
- public static Array StrToArrayEx(this string str, string separator = ",")
- {
- ArrayList arrayList = new ArrayList();
- str = str == null ? "" : str.Trim();
- int length1 = separator.Length;
- int length2;
- for (; str.Length > 0; str = length2 + length1 <= str.Length ? str.Substring(length2 + length1).Trim() : "")
- {
- length2 = str.IndexOf(separator, StringComparison.Ordinal);
- int num1 = str.IndexOf("[", StringComparison.Ordinal);
- if (num1 >= 0 && num1 < length2)
- {
- int num2 = 1;
- int length3 = str.Length;
- int startIndex;
- for (startIndex = num1 + 1; startIndex < length3; ++startIndex)
- {
- switch (str[startIndex])
- {
- case '[':
- ++num2;
- break;
- case ']':
- --num2;
- break;
- }
- if (num2 == 0)
- break;
- }
- length2 = str.IndexOf(separator, startIndex, StringComparison.Ordinal);
- }
- if (length2 < 0)
- length2 = str.Length;
- string str1 = str.Substring(0, length2);
- int length4 = str1.Length;
- if (length4 > 0 && str1[0] == '[' && str1[length4 - 1] == ']')
- str1 = str1.Substring(1, length4 - 2);
- arrayList.Add(str1.Trim());
- }
- Array instance = new string[arrayList.Count];
- arrayList.CopyTo(instance);
- return instance;
- }
- /// <summary>
- /// 数组转字符串 (分隔符:,)
- /// </summary>
- /// <param name="paStr"></param>
- /// <param name="delimited"></param>
- /// <returns></returns>
- public static string ArrayToStr(this Array paStr, bool delimited = false)
- {
- return ArrayToStr(paStr, ",", delimited);
- }
- /// <summary>
- /// 数组转字符串
- /// </summary>
- /// <param name="paStr"></param>
- /// <param name="separator"></param>
- /// <param name="delimited">是否加界定符“[]”</param>
- /// <returns></returns>
- public static string ArrayToStr(this Array paStr, string separator, bool delimited = false)
- {
- string str1 = "";
- foreach (string str2 in paStr)
- str1 = str1 + (str1 == "" ? "" : separator) + (delimited ? GetDelimitedStr(str2) : str2);
- return str1;
- }
- /// <summary>
- /// 加上界定符
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static string GetDelimitedStr(object obj)
- {
- return "[" + obj + "]";
- }
- /// <summary>
- /// 改编
- /// </summary>
- /// <param name="arr"></param>
- /// <param name="from"></param>
- /// <param name="to"></param>
- /// <returns></returns>
- public static Array RearrangeArray(this Array arr, int from, int to)
- {
- Array instance = new object[arr.Length];
- arr.CopyTo(instance, 0);
- object obj = instance.GetValue(from);
- if (from > to)
- {
- for (int index = from - 1; index >= to; --index)
- instance.SetValue(instance.GetValue(index), index + 1);
- }
- else
- {
- for (int index = from + 1; index <= to; ++index)
- instance.SetValue(instance.GetValue(index), index - 1);
- }
- instance.SetValue(obj, to);
- return instance;
- }
- /// <summary>
- /// 数组转数组List
- /// </summary>
- /// <param name="arr"></param>
- /// <returns></returns>
- public static ArrayList ArrayToArrayList(this Array arr)
- {
- ArrayList arrayList = new ArrayList();
- foreach (object obj in arr)
- arrayList.Add(obj);
- return arrayList;
- }
- /// <summary>
- /// 移除
- /// </summary>
- /// <param name="arr"></param>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static Array RemoveFromArray(this Array arr, object obj)
- {
- ArrayList arrayList = ArrayToArrayList(arr);
- arrayList.Remove(obj);
- Array instance = new object[arrayList.Count];
- arrayList.CopyTo(instance);
- return instance;
- }
- /// <summary>
- /// 数组list 插入(按字符串长度排序)
- /// </summary>
- /// <param name="poArrayList"></param>
- /// <param name="pcName"></param>
- public static void InsertIdentifier(this ArrayList poArrayList, string pcName)
- {
- int index = 0;
- while (index < poArrayList.Count)
- {
- string str = poArrayList[index].ToString();
- if (str == pcName)
- {
- index = -1;
- break;
- }
- if (str.Length < pcName.Length)
- {
- break;
- }
- index++;
- }
- if (index >= 0)
- {
- poArrayList.Insert(index, pcName);
- }
- }
- public static string MappingFromArrayList(this ICollection poArray)
- {
- string source = "";
- foreach (IwbIdName name in poArray)
- {
- string toBeAppended = $"[{name.Id}]=[{name.Name}]";
- source = AddStr(source, toBeAppended);
- }
- return source;
- }
- public static ArrayList MappingToArrayList(this string pcMapping)
- {
- return MappingToArrayList(pcMapping, ",");
- }
- public static ArrayList MappingToArrayList(this string pcMapping, string pcSeparator)
- {
- ArrayList list = new ArrayList();
- foreach (string str in StrToArrayEx(pcMapping, pcSeparator))
- {
- Array array2 = StrToArrayEx(str, "=");
- if (array2.Length == 2)
- {
- list.Add(new IwbIdName(array2.GetValue(0).ToString(), array2.GetValue(1).ToString()));
- }
- }
- return list;
- }
- public static IdCollection MappingToIdCollection(this string pcMapping)
- {
- return MappingToIdCollection(pcMapping, ",");
- }
- public static IdCollection MappingToIdCollection(this string pcMapping, string pcSeparator)
- {
- IdCollection ids = new IdCollection();
- foreach (string str in StrToArrayEx(pcMapping, pcSeparator))
- {
- Array array2 = StrToArrayEx(str, "=");
- if (array2.Length == 2)
- {
- ids.Add(new IwbIdName(array2.GetValue(0).ToString(), array2.GetValue(1).ToString()));
- }
- }
- return ids;
- }
- public static string MergeCriteriaMapping(this string pcCriteria1, string pcCriteria2)
- {
- string source = "";
- Array array = StrToArray(pcCriteria1);
- Array array2 = StrToArray(pcCriteria2);
- foreach (string str2 in array)
- {
- source = AppendToStr(source, str2);
- }
- foreach (string str3 in array2)
- {
- source = AppendToStr(source, str3);
- }
- return source;
- }
- #endregion Array
- #region DateTime
- /// <summary>
- /// 时间转字符串
- /// </summary>
- /// <param name="dateTime"></param>
- /// <returns></returns>
- public static string DtToStr(this DateTime dateTime)
- {
- return dateTime.ToString("s").Replace("T", " ");
- }
- /// <summary>
- /// 字符串转时间
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static DateTime StrToDt(this string str)
- {
- DateTime dateTime = new DateTime(1900, 1, 1);
- try
- {
- dateTime = DateTime.Parse(str);
- }
- catch (Exception)
- {
- // ignored
- }
- return dateTime;
- }
- /// <summary>
- /// 日期转指定格式日期(默认yyyyMMddHHmmss)
- /// </summary>
- /// <param name="dateTime"></param>
- /// <param name="formatter"></param>
- /// <returns></returns>
- public static string DtToStrFormatter(this DateTime dateTime, string formatter = "yyyyMMddHHmmss")
- {
- return dateTime.ToString(formatter);
- }
- /// <summary>
- /// 日期转时间字符串
- /// </summary>
- /// <param name="dateTime"></param>
- /// <returns></returns>
- public static string DtToTimeStr(this DateTime dateTime)
- {
- return dateTime.ToString("T");
- }
- /// <summary>
- /// 日期转日期字符串
- /// </summary>
- /// <param name="dateTime"></param>
- /// <returns></returns>
- public static string DtToDateStr(this DateTime dateTime)
- {
- return dateTime.ToString("d");
- }
- /// <summary>
- /// 获取秒
- /// </summary>
- /// <param name="dateTime"></param>
- /// <returns></returns>
- public static int DtToSeconds(this DateTime dateTime)
- {
- return dateTime.TimeOfDay.Seconds;
- }
- /// <summary>
- /// 今天之后多少秒
- /// </summary>
- /// <param name="seconds"></param>
- /// <returns></returns>
- public static DateTime DtFromSeconds(int seconds)
- {
- return DateTime.Today + new TimeSpan(0, 0, seconds);
- }
- /// <summary>
- /// 时间差(分)
- /// </summary>
- /// <param name="start"></param>
- /// <param name="end"></param>
- /// <returns></returns>
- public static int GetTimeSpanMinute(this DateTime start, DateTime end)
- {
- return Convert.ToInt32(GetTimeSpan(start, end).TotalMinutes);
- }
- /// <summary>
- /// 时间差
- /// </summary>
- /// <param name="start"></param>
- /// <param name="end"></param>
- /// <returns></returns>
- public static TimeSpan GetTimeSpan(this DateTime start, DateTime end)
- {
- TimeSpan timeSpan = end - start;
- return timeSpan;
- }
- #endregion DateTime
- }
- }
|