You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.8 KiB
62 lines
1.8 KiB
using System;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using HtmlAgilityPack;
|
|
|
|
namespace Infrastructure.Extensions
|
|
{
|
|
public static class StringExtensions
|
|
{
|
|
public static string Format(this string value, params object[] args)
|
|
{
|
|
return string.Format(value, args);
|
|
}
|
|
|
|
public static string GetExtension(this string filename)
|
|
{
|
|
return filename.Substring(filename.LastIndexOf('.') + 1).ToLower();
|
|
}
|
|
|
|
public static string GetSummary(this string value, int length = 32)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
{
|
|
return value;
|
|
}
|
|
if (value.Length > length)
|
|
{
|
|
return value.Substring(0, length) + "...";
|
|
}
|
|
return value;
|
|
}
|
|
|
|
public static string HtmlToText(this string html, int length = 10)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(html))
|
|
{
|
|
return html;
|
|
}
|
|
var result = string.Empty;
|
|
var doc = new HtmlDocument();
|
|
doc.LoadHtml(html);
|
|
var builder = new StringBuilder();
|
|
builder.Append(result);
|
|
foreach (var item in doc.DocumentNode.ChildNodes)
|
|
{
|
|
builder.Append(item.InnerText);
|
|
if (result.Length >= length)
|
|
{
|
|
result = result.Substring(0, length);
|
|
break;
|
|
}
|
|
}
|
|
result = builder.ToString();
|
|
return result;
|
|
}
|
|
|
|
public static byte[] StringToByteArray(this string hex)
|
|
{
|
|
return Enumerable.Range(0, hex.Length / 2).Select(x => Convert.ToByte(hex.Substring(x * 2, 2), 16)).ToArray();
|
|
}
|
|
}
|
|
} |