mirror of
https://github.com/nsnail/ns-ext.git
synced 2025-04-20 01:22:51 +08:00
Merge branch 'dev' of https://github.com/nsnail/ns-ext into dev
This commit is contained in:
commit
13f8ae51c2
@ -1,10 +1,10 @@
|
||||
# ns-ext
|
||||
|
||||
[中](README.zh-CN.md) | **En**
|
||||
The **ns-ext** is a .NET extension function library, containing the following types of extension modules:
|
||||
|
||||
|
||||
| Features | File name |
|
||||
| -------- | ---------------------------------- |
|
||||
|------------------------------------------|------------------------------------|
|
||||
| Byte type extension | ByteExtensions.cs |
|
||||
| Character Type Extensions | CharExtensions.cs |
|
||||
| Date Type Extensions | DateTimeExtensions.cs |
|
||||
|
@ -4,10 +4,16 @@ namespace NSExt.Constant;
|
||||
// 使用 RegexGenerator 新特性会生成重复key值的xmlcomment导致出错
|
||||
internal static class Regexes
|
||||
{
|
||||
public static readonly Regex RegexBacksLantUnicode
|
||||
= new("\\\\u([a-fA-F0-9]{4})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
|
||||
public static readonly Regex RegexHtmlTag = new("<[^>]*>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
|
||||
public static readonly Regex RegexMobile
|
||||
= new("^(\\d{3})\\d{4}(\\d{4})$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
|
||||
public static readonly Regex RegexPercentUnicode
|
||||
= new("\\\\u([a-fA-F0-9]{4})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
|
||||
public static readonly Regex RegexUpLetter = new("([A-Z])", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
}
|
@ -42,17 +42,20 @@ public static class ByteExtensions
|
||||
/// <param name="me">me</param>
|
||||
/// <param name="upperCase">是否大写</param>
|
||||
/// <param name="splitShar">字节间分隔符</param>
|
||||
public static string String(this byte[] me, bool upperCase = true, string splitShar = null)
|
||||
/// <param name="splitInterval">分隔符跳跃字节数</param>
|
||||
public static string String(this IEnumerable<byte> me, bool upperCase = true, string splitShar = ""
|
||||
, int splitInterval = 1)
|
||||
{
|
||||
var ret = BitConverter.ToString(me);
|
||||
if (!upperCase) {
|
||||
ret = ret.ToLower(CultureInfo.InvariantCulture);
|
||||
var sb = new StringBuilder();
|
||||
var i = 0;
|
||||
foreach (var c in me.Select(x => x.ToString(upperCase ? "X2" : "x2", CultureInfo.InvariantCulture))) {
|
||||
if (i++ % splitInterval == 0) {
|
||||
sb.Append(splitShar);
|
||||
}
|
||||
|
||||
if (splitShar != "-") {
|
||||
ret = ret.Replace("-", splitShar ?? string.Empty);
|
||||
sb.Append(c);
|
||||
}
|
||||
|
||||
return ret;
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
@ -508,6 +508,16 @@ public static class StringExtensions
|
||||
return ret == me ? ret : ret.TrimSpaces();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将\ux0000 、 %u0000 、 &#x0000; 编码转换成可读字符串
|
||||
/// </summary>
|
||||
public static string UnicodeDe(this string me)
|
||||
{
|
||||
const string replacement = "&#x$1;";
|
||||
return me.Contains(@"\u") ? Regexes.RegexBacksLantUnicode.Replace(me, replacement).HtmlDe() :
|
||||
me.Contains(@"%u") ? Regexes.RegexPercentUnicode.Replace(me, replacement).HtmlDe() : me.HtmlDe();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// url编码
|
||||
/// </summary>
|
||||
|
Loading…
x
Reference in New Issue
Block a user