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
///
/// 检查空字符串
///
///
///
public static bool IsNotEmpty(this string str)
{
return !IsEmpty(str);
}
///
/// 检查空字符串
///
///
///
public static bool IsEmpty(this string str)
{
return string.IsNullOrEmpty(str);
}
///
/// 大写并移除空白字符串
///
///
///
public static string UAndT(this string str)
{
if (str == null)
str = "";
return str.ToUpper().Trim();
}
///
/// 大写并移除空白字符串
///
///
///
public static string UAndT(this object obj)
{
return UAndT(obj.ToString());
}
///
/// 检查是否以某字符串开头,不是加上此字符串
///
///
///
///
public static string Sw(this string str, string startStr)
{
return str.StartsWith(startStr) ? str : $"{startStr}{str}";
}
///
/// 检查是否以某字符串结尾,不是加上此字符串
///
///
///
///
public static string Ew(this string str, string endStr)
{
return str.EndsWith(endStr) ? str : $"{str}{endStr}";
}
///
/// 前补全
///
///
///
///
///
public static string LeftPad(this int i, int num, string charStr = "0")
{
return i.ToString().PadLeft(num, Convert.ToChar(charStr));
}
///
/// 后补全
///
///
///
///
///
public static string RightPad(this int i, int num, string charStr = "0")
{
return i.ToString().PadRight(num, Convert.ToChar(charStr));
}
///
/// 前补全
///
///
///
///
///
public static string LeftPad(this string s, int num, string charStr = "0")
{
return s.PadLeft(num, Convert.ToChar(charStr));
}
///
/// 后补全
///
///
///
///
///
public static string RightPad(this string s, int num, string charStr = "0")
{
return s.PadRight(num, Convert.ToChar(charStr));
}
///
/// 复制
///
///
/// 复制次数
///
public static string Replicate(this string str, int count)
{
string str2 = "";
for (int index = 0; index < count; ++index)
str2 += str;
return str2;
}
///
/// 末尾移除匹配
///
///
///
///
public static string TrimEnd(string source, string removeStr)
{
char[] charArray = removeStr.ToCharArray();
return source.TrimEnd(charArray);
}
///
/// 截取字符串中开始和结束字符串中间的字符串
///
/// 源字符串
/// 开始字符串
/// 结束字符串
/// 中间字符串
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;
}
///
/// (批量)截取字符串中开始和结束字符串中间的字符串
///
/// 源字符串
/// 开始字符串
/// 结束字符串
/// 中间字符串
public static List 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 resList = new List();
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
///
/// 验证int,并返回
///
///
///
public static int ValI(this string str)
{
return (int)ValD(str);
}
///
/// 验证decimal,并返回
///
///
///
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);
}
///
/// int转bool (true,yes,y,>1)
///
///
///
public static bool ValB(this int i)
{
var str = i > 0 ? "1" : "0";
return StrToBool(str);
}
///
/// 字符串转bool (true,yes,y,1)
///
///
///
public static bool ValB(this string str)
{
return StrToBool(str);
}
///
/// 字符串转bool (true,yes,y,1)
///
///
///
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
///
/// 字符转数组
///
///
/// 分隔符默认:,
///
public static string[] StrToArray(this string str, string separator = ",")
{
return (string[])StrToArrayEx(str, separator);
}
///
/// 字符转数组
///
///
/// 分隔符默认:,
///
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;
}
///
/// 数组转字符串 (分隔符:,)
///
///
///
///
public static string ArrayToStr(this Array paStr, bool delimited = false)
{
return ArrayToStr(paStr, ",", delimited);
}
///
/// 数组转字符串
///
///
///
/// 是否加界定符“[]”
///
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;
}
///
/// 加上界定符
///
///
///
public static string GetDelimitedStr(object obj)
{
return "[" + obj + "]";
}
///
/// 改编
///
///
///
///
///
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;
}
///
/// 数组转数组List
///
///
///
public static ArrayList ArrayToArrayList(this Array arr)
{
ArrayList arrayList = new ArrayList();
foreach (object obj in arr)
arrayList.Add(obj);
return arrayList;
}
///
/// 移除
///
///
///
///
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;
}
///
/// 数组list 插入(按字符串长度排序)
///
///
///
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
///
/// 时间转字符串
///
///
///
public static string DtToStr(this DateTime dateTime)
{
return dateTime.ToString("s").Replace("T", " ");
}
///
/// 字符串转时间
///
///
///
public static DateTime StrToDt(this string str)
{
DateTime dateTime = new DateTime(1900, 1, 1);
try
{
dateTime = DateTime.Parse(str);
}
catch (Exception)
{
// ignored
}
return dateTime;
}
///
/// 日期转指定格式日期(默认yyyyMMddHHmmss)
///
///
///
///
public static string DtToStrFormatter(this DateTime dateTime, string formatter = "yyyyMMddHHmmss")
{
return dateTime.ToString(formatter);
}
///
/// 日期转时间字符串
///
///
///
public static string DtToTimeStr(this DateTime dateTime)
{
return dateTime.ToString("T");
}
///
/// 日期转日期字符串
///
///
///
public static string DtToDateStr(this DateTime dateTime)
{
return dateTime.ToString("d");
}
///
/// 获取秒
///
///
///
public static int DtToSeconds(this DateTime dateTime)
{
return dateTime.TimeOfDay.Seconds;
}
///
/// 今天之后多少秒
///
///
///
public static DateTime DtFromSeconds(int seconds)
{
return DateTime.Today + new TimeSpan(0, 0, seconds);
}
///
/// 时间差(分)
///
///
///
///
public static int GetTimeSpanMinute(this DateTime start, DateTime end)
{
return Convert.ToInt32(GetTimeSpan(start, end).TotalMinutes);
}
///
/// 时间差
///
///
///
///
public static TimeSpan GetTimeSpan(this DateTime start, DateTime end)
{
TimeSpan timeSpan = end - start;
return timeSpan;
}
#endregion DateTime
}
}