using Ewide.RoadFlowLite.Utility;
using Furion;
using Furion.DataEncryption;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace RoadFlow.Utility
{
///
/// 字符串操作扩展类
///
public static class StringExtensions
{
///
/// 判断字符串是否为null或""
///
///
///
public static bool IsNullOrEmpty(this string str)
{
return string.IsNullOrEmpty(str);
}
///
/// 判断字符串是否为null或""或" "(包含空字符的字符串)
///
///
///
public static bool IsNullOrWhiteSpace(this string str)
{
return string.IsNullOrWhiteSpace(str);
}
//判断StringValues是否为空
public static bool IsNullOrEmpty(this StringValues sv)
{
return StringValues.IsNullOrEmpty(sv);
}
///
/// 比较字符串区分大小写
///
/// 字符串1
/// 字符串2
///
public static bool EqualsIgnoreCase(this string str1, string str2)
{
return null == str1 ? null == str2 : str1.Equals(str2, StringComparison.CurrentCultureIgnoreCase);
}
///
/// 判断字符串是否包含(不区分大不写)
///
///
///
///
public static bool ContainsIgnoreCase(this string str1, string str2)
{
return null == str1 || null == str2 ? false : str1.IndexOf(str2, StringComparison.CurrentCultureIgnoreCase) >= 0;
}
///
/// 去除空格
///
///
///
public static string Trim1(this string str)
{
return str.IsNullOrEmpty() ? "" : str.Trim();
}
///
/// 将字符串转换为GUID
///
///
///
public static Guid ToGuid(this string str)
{
return Guid.TryParse(str.Trim1(), out Guid guid) ? guid : Guid.Empty;
}
///
/// 判断一个字符串是否是GUID
///
///
///
public static bool IsGuid(this string str)
{
return Guid.TryParse(str.Trim1(), out _);
}
///
/// 判断一个字符串是否是GUID
///
///
///
public static bool IsGuid(this string str, out Guid guid)
{
return Guid.TryParse(str.Trim1(), out guid);
}
///
/// 判断一个字符串是否是字体图标(以fa开头)
///
///
///
public static bool IsFontIco(this string str)
{
return str.Trim1().StartsWith("fa");
}
///
/// 判断字符串是否为整数
///
///
///
public static bool IsInt(this string str)
{
return int.TryParse(str, out _);
}
///
/// 判断字符串是否为整数
///
///
///
public static bool IsInt(this string str, out int i)
{
return int.TryParse(str, out i);
}
///
/// 将字符串转换为整数
///
///
/// 转换失败时的默认值
///
public static int ToInt(this string str, int defaultValue = int.MinValue)
{
return int.TryParse(str, out int i) ? i : defaultValue;
}
///
/// 判断字符串是否是长整型
///
///
///
public static bool IsLong(this string str)
{
return long.TryParse(str, out _);
}
///
/// 判断字符串是否是长整型
///
///
///
public static bool IsLong(this string str, out long l)
{
return long.TryParse(str, out l);
}
///
/// 将字符串转换为长整型
///
///
/// 转换失败后的默认值
///
public static long ToLong(this string str, long defauleValue = long.MinValue)
{
return long.TryParse(str, out long l) ? l : defauleValue;
}
///
/// 判断字符串是否为数字
///
///
///
public static bool IsDecimal(this string str)
{
return decimal.TryParse(str, out _);
}
///
/// 判断字符串是否为数字
///
///
///
public static bool IsDecimal(this string str, out decimal d)
{
return decimal.TryParse(str, out d);
}
///
/// 将字符串转换为数字
///
///
/// 转换失败时的默认值
///
public static decimal ToDecimal(this string str, decimal defaultValue = decimal.MinValue)
{
return decimal.TryParse(str, out decimal d) ? d : defaultValue;
}
///
/// 将字符串MD5加密
///
///
///
public static string MD5(this string str)
{
return MD5Encryption.Encrypt(str.Trim1());
//return Encryption.MD5(str.Trim1());
}
///
/// 判断字符串是否为日期时间
///
///
///
public static bool IsDateTime(this string str)
{
return DateTime.TryParse(str, out DateTime dt);
}
///
/// 判断字符串是否为日期时间
///
///
///
public static bool IsDateTime(this string str, out DateTime dt)
{
return DateTime.TryParse(str, out dt);
}
///
/// 将字符串转换为日期时间
///
///
///
public static DateTime ToDateTime(this string str)
{
return DateTime.TryParse(str, out DateTime dt) ? dt : DateTime.MinValue;
}
///
/// 验证字符串是否为数字
///
///
///
public static bool IsDigital(this string str)
{
foreach (char c in str.ToCharArray())
{
if (!char.IsDigit(c))
{
return false;
}
}
return true;
}
///
/// 验证是否为固话号码
///
///
///
public static bool IsTelNumber(this string str)
{
//去掉-线后全为数字
return str.IsNullOrWhiteSpace() ? false : !str.StartsWith("-") && str.Replace("-", "").IsDigital();
}
///
/// 去掉组织机构人员前缀
///
///
public static string RemoveUserPrefix(this string str)
{
return str.IsNullOrWhiteSpace() ? "" : str.StartsWith("u_") ? str.TrimStart('u', '_') : str;
}
///
/// 去掉组织机构工作组前缀
///
///
public static string RemoveWorkGroupPrefix(this string str)
{
return str.IsNullOrWhiteSpace() ? "" : str.StartsWith("w_") ? str.TrimStart('w', '_') : str;
}
///
/// 去掉组织机构人员兼职前缀
///
///
public static string RemoveUserRelationPrefix(this string str)
{
return str.IsNullOrWhiteSpace() ? "" : str.StartsWith("r_") ? str.TrimStart('r', '_') : str;
}
///
/// 移出所有空格
///
///
///
public static string TrimAll(this string str)
{
return Regex.Replace(str, @"\s", "");
}
///
/// 转换为SQL的in字符串
///
/// 逗号分开的字符串
/// 是否加单引号
///
public static string ToSqlIn(this string str, bool isSignle = true)
{
if (str.IsNullOrEmpty())
{
return string.Empty;
}
StringBuilder stringBuilder = new StringBuilder();
foreach (string s in str.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
if (isSignle)
{
stringBuilder.Append("'");
}
stringBuilder.Append(s);
if (isSignle)
{
stringBuilder.Append("'");
}
stringBuilder.Append(",");
}
return stringBuilder.ToString().TrimEnd(',');
}
#region 得到汉字拼音
///
/// 得到汉字拼音(全拼)
///
///
///
public static string ToPinYing(this string str)
{
if (string.IsNullOrWhiteSpace(str))
{
return "";
}
var format = new Pinyin.format.PinyinOutputFormat(Pinyin.format.ToneFormat.WITHOUT_TONE,
Pinyin.format.CaseFormat.LOWERCASE, Pinyin.format.VCharFormat.WITH_U_AND_COLON);
return Pinyin.Pinyin4Net.GetPinyin(str, format).TrimAll();
}
#endregion
/*
///
/// URL编码
///
///
///
public static string UrlEncode(this string url)
{
//return WebUtility.UrlEncode(url).Replace("+", "%20")
// .Replace("*", "%2A")
// .Replace("%7E", "~")
// .Replace("!", "%21")
// //.Replace("'", "%27")
// .Replace("(", "%28")
// .Replace(")", "%29");
return Uri.EscapeDataString(url);
}
*/
///
/// URL解码
///
///
///
public static string UrlDecode(this string url)
{
//return WebUtility.UrlDecode(url);
return Uri.UnescapeDataString(url);
}
///
/// HTML编码
///
///
///
public static string HtmlEncode(this string str)
{
return WebUtility.HtmlEncode(str);
}
///
/// HTML解码
///
///
///
public static string HtmlDecode(this string str)
{
return WebUtility.HtmlDecode(str);
}
///
/// 将List拼接为字符串
///
///
/// 分隔符
/// 前缀
/// 后缀
///
public static string JoinList(this IEnumerable ts, string split = ",", string prefix = "", string suffix = "")
{
if (null == ts || !ts.Any())
{
return "";
}
StringBuilder stringBuilder = new StringBuilder();
foreach (var t in ts)
{
stringBuilder.Append(prefix);
stringBuilder.Append(t);
stringBuilder.Append(suffix);
stringBuilder.Append(split);
}
return stringBuilder.ToString().TrimEnd(split.ToCharArray()).FilterSelectSql();
}
///
/// 将List转换为SQL in语句
///
///
///
/// 是包含单引号
///
public static string JoinSqlIn(this List ts, bool single = true)
{
return ts.JoinList(",", single ? "'" : "", single ? "'" : "");
}
///
/// 得到实符串实际长度
///
///
///
public static int Size(this string str)
{
byte[] strArray = Encoding.Default.GetBytes(str);
return strArray.Length;
}
///
/// 去除HTML标记
///
/// 包括HTML的源码
/// 已经去除后的文字
public static string RemoveHTML(this string Htmlstring)
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"", "", RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"