//------------------------------------------------------------------------------ // 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有 // 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权 // CSDN博客:https://blog.csdn.net/qq_40374647 // 哔哩哔哩视频:https://space.bilibili.com/94253567 // Gitee源代码仓库:https://gitee.com/RRQM_Home // Github源代码仓库:https://github.com/RRQM // API首页:https://www.yuque.com/rrqm/touchsocket/index // 交流QQ群:234762506 // 感谢您的下载和使用 //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ using System; using System.Collections.Generic; using System.ComponentModel; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; namespace TouchSocket.Core { /// /// StringExtension /// public static class StringExtension { /// /// IsNullOrEmpty /// /// /// public static bool IsNullOrEmpty(this string str) { return string.IsNullOrEmpty(str); } /// /// IsNullOrWhiteSpace /// /// /// public static bool IsNullOrWhiteSpace(this string str) { return string.IsNullOrWhiteSpace(str); } /// /// 当不为null,且不为空。 /// /// /// public static bool HasValue(this string str) { return !string.IsNullOrWhiteSpace(str); } /// /// 将字符串格式化成指定的数据类型 /// /// /// /// /// /// public static bool TryParseToType(string str, Type type, out object value, char[] splits = default) { if (string.IsNullOrEmpty(str)) { value = default; return true; } if (type == null) { value = str; return true; } if (type.IsArray) { Type elementType = type.GetElementType(); string[] strs; if (splits == null) { strs = str.Split(new char[] { ' ', ';', '-', '/' }); } else { strs = str.Split(splits); } Array array = Array.CreateInstance(elementType, strs.Length); for (int i = 0, c = strs.Length; i < c; ++i) { object o; if (ConvertSimpleType(strs[i], elementType, out o)) { array.SetValue(o, i); } else { value = default; return false; } } value = array; return true; } return ConvertSimpleType(str, type, out value); } private static bool ConvertSimpleType(string value, Type destinationType, out object returnValue) { if ((value == null) || destinationType.IsInstanceOfType(value)) { returnValue = value; return true; } if (string.IsNullOrEmpty(value)) { returnValue = default; return true; } TypeConverter converter = TypeDescriptor.GetConverter(destinationType); bool flag = converter.CanConvertFrom(value.GetType()); if (!flag) { converter = TypeDescriptor.GetConverter(value.GetType()); } if (!flag && !converter.CanConvertTo(destinationType)) { returnValue = default; return false; } try { returnValue = flag ? converter.ConvertFrom(null, null, value) : converter.ConvertTo(null, null, value, destinationType); } catch { returnValue = default; return false; } return true; } /// /// 判断字符串compare 在 input字符串中出现的次数 /// /// 源字符串 /// 用于比较的字符串 /// 字符串compare 在 input字符串中出现的次数 public static int HitStringCount(this string input, string compare) { int index = input.IndexOf(compare); if (index != -1) { return 1 + HitStringCount(input.Substring(index + compare.Length), compare); } else { return 0; } } /// /// 将字符转换为对应的基础类型类型。 /// /// /// 目标类型必须为基础类型 /// public static object ParseToType(this string value, Type destinationType) { object returnValue; if ((value == null) || destinationType.IsInstanceOfType(value)) { return value; } string str = value; if ((str != null) && (str.Length == 0)) { return null; } TypeConverter converter = TypeDescriptor.GetConverter(destinationType); bool flag = converter.CanConvertFrom(value.GetType()); if (!flag) { converter = TypeDescriptor.GetConverter(value.GetType()); } if (!flag && !converter.CanConvertTo(destinationType)) { throw new InvalidOperationException("无法转换成类型:" + value.ToString() + "==>" + destinationType); } try { returnValue = flag ? converter.ConvertFrom(null, null, value) : converter.ConvertTo(null, null, value, destinationType); } catch (Exception e) { throw new InvalidOperationException(" 类型转换出错:" + value.ToString() + "==>" + destinationType, e); } return returnValue; } /// /// 只按第一个匹配项分割 /// /// /// /// public static string[] SplitFirst(this string str, char split) { List s = new List(); int index = str.IndexOf(split); if (index > 0) { s.Add(str.Substring(0, index).Trim()); s.Add(str.Substring(index + 1, str.Length - index - 1).Trim()); } return s.ToArray(); } /// /// 按字符串分割 /// /// /// /// public static string[] Split(this string str, string pattern) { return Regex.Split(str, pattern); } /// /// 只按最后一个匹配项分割 /// /// /// /// public static string[] SplitLast(this string str, char split) { List s = new List(); int index = str.LastIndexOf(split); if (index > 0) { s.Add(str.Substring(0, index).Trim()); s.Add(str.Substring(index + 1, str.Length - index - 1).Trim()); } return s.ToArray(); } /// /// 按格式填充 /// /// /// /// public static string Format(this string str, params object[] ps) { if (ps == null || ps.Length == 0) { return str; } try { return string.Format(str, ps); } catch { return str; } } /// /// 转换为SHA1。 /// /// /// /// public static byte[] ToSha1(this string value, Encoding encoding) { using (SHA1 sha1 = SHA1.Create()) { return sha1.ComputeHash(encoding.GetBytes(value)); } } /// /// 转换为UTF-8数据,效果等于 /// /// /// public static byte[] ToUTF8Bytes(this string value) { return Encoding.UTF8.GetBytes(value); } /// /// 将16进制的字符转换为数组。 /// /// /// /// public static byte[] ByHexStringToBytes(this string hexString, string splite = default) { if (!string.IsNullOrEmpty(splite)) { hexString = hexString.Replace(splite, string.Empty); } if ((hexString.Length % 2) != 0) { hexString += " "; } byte[] returnBytes = new byte[hexString.Length / 2]; for (int i = 0; i < returnBytes.Length; i++) { returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); } return returnBytes; } /// /// 将16进制的字符转换为int32。 /// /// /// public static int ByHexStringToInt32(this string hexString) { if (string.IsNullOrEmpty(hexString)) { return default; } return int.Parse(hexString, System.Globalization.NumberStyles.HexNumber); } /// /// 从Base64转到数组。 /// /// /// public static byte[] ByBase64ToBytes(this string value) { return Convert.FromBase64String(value); } } }