接口文件

This commit is contained in:
“wanyongkang”
2024-04-10 13:55:27 +08:00
parent fff6bee06a
commit ed3b2c653e
3190 changed files with 268248 additions and 1 deletions

View File

@@ -0,0 +1,779 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using TinyPinyin.Core;
namespace Hncore.Infrastructure.Extension
{
public static class StringExtension
{
#region UrlHtml编码
[DebuggerStepThrough]
public static string UrlEncode(this string target)
{
return HttpUtility.UrlEncode(target);
}
[DebuggerStepThrough]
public static string UrlEncode(this string target, Encoding encoding)
{
return HttpUtility.UrlEncode(target, encoding);
}
[DebuggerStepThrough]
public static string UrlDecode(this string target)
{
return HttpUtility.UrlDecode(target);
}
[DebuggerStepThrough]
public static string UrlDecode(this string target, Encoding encoding)
{
return HttpUtility.UrlDecode(target, encoding);
}
[DebuggerStepThrough]
public static string AttributeEncode(this string target)
{
return HttpUtility.HtmlAttributeEncode(target);
}
[DebuggerStepThrough]
public static string HtmlEncode(this string target)
{
return HttpUtility.HtmlEncode(target);
}
[DebuggerStepThrough]
public static string HtmlDecode(this string target)
{
return HttpUtility.HtmlDecode(target);
}
#endregion
#region Unicode编码
/// <summary>
/// 汉字转换为Unicode编码
/// </summary>
/// <param name="str">要编码的汉字字符串</param>
/// <returns>Unicode编码的的字符串</returns>
public static string ToUnicode(this string str)
{
if (string.IsNullOrEmpty(str))
{
return str;
}
StringBuilder unicode = new StringBuilder();
foreach (char chr in str)
{
// Get the integral value of the character.
int value = Convert.ToInt32(chr);
// Convert the decimal value to a hexadecimal value in string form.
string hexOutput = String.Format("{0:x}", value);
unicode.Append("\\u" + hexOutput);
}
return unicode.ToString();
}
/// <summary>
/// 将Unicode编码转换为汉字字符串
/// </summary>
/// <param name="str">Unicode编码字符串</param>
/// <returns>汉字字符串</returns>
public static string FromUnicode(this string unicode)
{
if (string.IsNullOrEmpty(unicode))
{
return unicode;
}
StringBuilder str = new StringBuilder();
unicode = unicode.Replace("\\u", "_");
string[] hex = unicode.Split('_');
for (int i = 1; i < hex.Length; i++)
{
int data = Convert.ToInt32(hex[i].ToString(), 16);
str.Append((char) data);
}
return str.ToString();
}
#endregion
#region
/// <summary>
/// 清除脚本
/// </summary>
/// <param name="Htmlstring"></param>
/// <returns></returns>
public static string NoHTML(this string Htmlstring)
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<.*?>|&.{4,5}", "", RegexOptions.IgnoreCase);
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, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("\r\n", "");
Htmlstring = Htmlstring.HtmlEncode().Trim();
return Htmlstring;
}
#endregion
#region 0
/// <summary>
/// 清楚小数点后的0
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string DecimalToInt(this string str)
{
if (string.IsNullOrEmpty(str))
return "0";
str = float.Parse(str).ToString("0.00");
if (Int32.Parse(str.Substring(str.IndexOf(".") + 1)) == 0)
{
return str.Substring(0, str.IndexOf("."));
}
return str;
}
public static string ToMoney(this decimal num)
{
return num.ToString("0.00");
}
#endregion
#region
/// <summary>
/// 转换百分比
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public static string ToP2(this decimal num)
{
string str = num.ToString("0.00").TrimEnd('0');
if (str.EndsWith("."))
{
str = str.Replace(".", "");
}
return str + "%";
}
#endregion
#region
/// <summary>
/// 汉字转拼音缩写
/// <returns>拼音缩写</returns>
public static string GetPYString(this string str)
{
string tempStr = "";
foreach (char c in str)
{
if ((int) c >= 33 && (int) c <= 126)
{
//字母和符号原样保留
tempStr += c.ToString();
}
else
{
//累加拼音声母
tempStr += PinyinHelper.GetPinyin(c).ToCharArray()[0];
}
}
return tempStr;
}
#endregion
#region \r\n\t
/// <summary>
/// 清楚字符串中所有空格,包括\r\n\t
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string CleanSpace(this string str)
{
if (string.IsNullOrEmpty(str))
return string.Empty;
return Regex.Replace(str, @"\s", "");
}
#endregion
#region
/// <summary>
/// 反转义
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Unescape(this string str)
{
return System.Text.RegularExpressions.Regex.Unescape(str);
}
#endregion
#region
public static int ToInt(this string str)
{
int i = 0;
int.TryParse(str, out i);
return i;
}
public static decimal ToDecimal(this string str)
{
decimal i = 0;
decimal.TryParse(str, out i);
return i;
}
public static int ToInt(this string str, int defaultnum = 0)
{
int i;
bool flag = Int32.TryParse(str, out i);
if (flag)
{
return i;
}
else
{
return defaultnum;
}
}
/// <summary>
/// 转为double类型
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static double ToDouble(this string str)
{
double result = 0;
double.TryParse(str, out result);
return result;
}
#endregion
#region , 12
/// <summary>
/// 返回字符串真实长度, 1个汉字长度为2
/// </summary>
/// <returns>字符长度</returns>
public static int GetStringLength(this string str)
{
return Encoding.Default.GetBytes(str).Length;
}
#endregion
#region
/// <summary>
/// 从字符串的指定位置截取指定长度的子字符串
/// </summary>
/// <param name="str">原字符串</param>
/// <param name="startIndex">子字符串的起始位置</param>
/// <param name="length">子字符串的长度</param>
/// <returns>子字符串</returns>
public static string CutString(this string str, int startIndex, int length)
{
if (string.IsNullOrEmpty(str))
{
return "";
}
if (startIndex >= 0)
{
if (length < 0)
{
length = length * -1;
if (startIndex - length < 0)
{
length = startIndex;
startIndex = 0;
}
else
{
startIndex = startIndex - length;
}
}
if (startIndex > str.Length)
{
return "";
}
}
else
{
if (length < 0)
{
return "";
}
else
{
if (length + startIndex > 0)
{
length = length + startIndex;
startIndex = 0;
}
else
{
return "";
}
}
}
if (str.Length - startIndex < length)
{
length = str.Length - startIndex;
}
return str.Substring(startIndex, length);
}
#endregion
#region
/// <summary>
/// 判断文件名是否为浏览器可以直接显示的图片文件名
/// </summary>
/// <param name="filename">文件名</param>
/// <returns>是否可以直接显示</returns>
public static bool IsImgFilename(this string filename)
{
filename = filename.Trim();
if (filename.EndsWith(".") || filename.IndexOf(".") == -1)
{
return false;
}
string extname = filename.Substring(filename.LastIndexOf(".") + 1).ToLower();
return (extname == "jpg" || extname == "jpeg" || extname == "png" || extname == "bmp" || extname == "gif");
}
#endregion
#region
public static string GetUnicodeSubString(this string str, int len, string p_TailString)
{
string result = string.Empty; // 最终返回的结果
int byteLen = System.Text.Encoding.Default.GetByteCount(str); // 单字节字符长度
int charLen = str.Length; // 把字符平等对待时的字符串长度
int byteCount = 0; // 记录读取进度
int pos = 0; // 记录截取位置
if (byteLen > len)
{
for (int i = 0; i < charLen; i++)
{
if (Convert.ToInt32(str.ToCharArray()[i]) > 255) // 按中文字符计算加2
byteCount += 2;
else // 按英文字符计算加1
byteCount += 1;
if (byteCount > len) // 超出时只记下上一个有效位置
{
pos = i;
break;
}
else if (byteCount == len) // 记下当前位置
{
pos = i + 1;
break;
}
}
if (pos >= 0)
result = str.Substring(0, pos) + p_TailString;
}
else
result = str;
return result;
}
/// <summary>
/// 取指定长度的字符串
/// </summary>
/// <param name="p_SrcString">要检查的字符串</param>
/// <param name="p_StartIndex">起始位置</param>
/// <param name="p_Length">指定长度</param>
/// <param name="p_TailString">用于替换的字符串</param>
/// <returns>截取后的字符串</returns>
public static string GetSubString(this string p_SrcString, int p_StartIndex, int p_Length, string p_TailString)
{
string myResult = p_SrcString;
Byte[] bComments = Encoding.UTF8.GetBytes(p_SrcString);
foreach (char c in Encoding.UTF8.GetChars(bComments))
{
//当是日文或韩文时(注:中文的范围:\u4e00 - \u9fa5, 日文在\u0800 - \u4e00, 韩文为\xAC00-\xD7A3)
if ((c > '\u0800' && c < '\u4e00') || (c > '\xAC00' && c < '\xD7A3'))
{
//if (System.Text.RegularExpressions.Regex.IsMatch(p_SrcString, "[\u0800-\u4e00]+") || System.Text.RegularExpressions.Regex.IsMatch(p_SrcString, "[\xAC00-\xD7A3]+"))
//当截取的起始位置超出字段串长度时
if (p_StartIndex >= p_SrcString.Length)
{
return "";
}
else
{
return p_SrcString.Substring(p_StartIndex,
((p_Length + p_StartIndex) > p_SrcString.Length)
? (p_SrcString.Length - p_StartIndex)
: p_Length);
}
}
}
if (p_Length >= 0)
{
byte[] bsSrcString = Encoding.Default.GetBytes(p_SrcString);
//当字符串长度大于起始位置
if (bsSrcString.Length > p_StartIndex)
{
int p_EndIndex = bsSrcString.Length;
//当要截取的长度在字符串的有效长度范围内
if (bsSrcString.Length > (p_StartIndex + p_Length))
{
p_EndIndex = p_Length + p_StartIndex;
}
else
{
//当不在有效范围内时,只取到字符串的结尾
p_Length = bsSrcString.Length - p_StartIndex;
p_TailString = "";
}
int nRealLength = p_Length;
int[] anResultFlag = new int[p_Length];
byte[] bsResult = null;
int nFlag = 0;
for (int i = p_StartIndex; i < p_EndIndex; i++)
{
if (bsSrcString[i] > 127)
{
nFlag++;
if (nFlag == 3)
{
nFlag = 1;
}
}
else
{
nFlag = 0;
}
anResultFlag[i] = nFlag;
}
if ((bsSrcString[p_EndIndex - 1] > 127) && (anResultFlag[p_Length - 1] == 1))
{
nRealLength = p_Length + 1;
}
bsResult = new byte[nRealLength];
Array.Copy(bsSrcString, p_StartIndex, bsResult, 0, nRealLength);
myResult = Encoding.Default.GetString(bsResult);
myResult = myResult + p_TailString;
}
}
return myResult;
}
#endregion
#region
/// <summary>
/// 自定义的替换字符串函数
/// </summary>
public static string ReplaceString(string SourceString, string SearchString, string ReplaceString,
bool IsCaseInsensetive)
{
return Regex.Replace(SourceString, Regex.Escape(SearchString), ReplaceString,
IsCaseInsensetive ? RegexOptions.IgnoreCase : RegexOptions.None);
}
#endregion
#region email格式
/// <summary>
/// 检测是否符合email格式
/// </summary>
/// <param name="strEmail">要判断的email字符串</param>
/// <returns>判断结果</returns>
public static bool IsValidEmail(this string strEmail)
{
return Regex.IsMatch(strEmail,
@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}
#endregion
#region Url
/// <summary>
/// 检测是否是正确的Url
/// </summary>
/// <param name="strUrl">要验证的Url</param>
/// <returns>判断结果</returns>
public static bool IsURL(this string strUrl)
{
return Regex.IsMatch(strUrl,
@"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
}
#endregion
#region ip
/// <summary>
/// 是否为ip
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static bool IsIP(this string ip)
{
return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
}
#endregion
#region
/// <summary>
/// 是否为手机号
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsMobilePhone(this string str)
{
return Regex.IsMatch(str, @"^0?(13[0-9]|15[0-9]|166|17[0-9]|18[0-9]|19[8-9]|14[5-7])[0-9]{8}$");
}
#endregion
#region
/// <summary>
/// 是否为身份证号
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsIdCard(this string str)
{
return Regex.IsMatch(str, @"(^\d{15}$)|(^\d{17}([0-9]|X)$)");
}
#endregion
#region
/// <summary>
/// 是否为小数
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsPositiveNumber(this string str)
{
return Regex.IsMatch(str, @"^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$");
}
#endregion
#region
/// <summary>
/// 是否为数字
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsNumber(this string str)
{
return Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$");
}
#endregion
#region
/// <summary>
/// 字符串转化为泛型集合
/// </summary>
/// <param name="str">字符串</param>
/// <param name="splitstr">要分割的字符</param>
/// <returns></returns>
public static List<T> StrToList<T>(this string str, char splitstr)
{
List<T> list = new List<T>();
if (string.IsNullOrEmpty(str))
{
return list;
}
string[] strarray = str.Split(new Char[] {splitstr}, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in strarray)
{
if (s != "")
list.Add((T) Convert.ChangeType(s, typeof(T)));
}
return list;
}
/// <summary>
/// 字符串转化为泛型集合
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
public static List<T> StrToList<T>(this string str)
{
return StrToList<T>(str, ',');
}
#endregion
public static string ToGBK(this string str)
{
return Encoding.GetEncoding("GBK").GetString(Encoding.Default.GetBytes(str));
}
public static string FromGBK(this string str)
{
return Encoding.Default.GetString(Encoding.GetEncoding("GBK").GetBytes(str));
}
public static string ToBase64String(this string value)
{
if (string.IsNullOrEmpty(value))
{
return "";
}
byte[] bytes = Encoding.UTF8.GetBytes(value);
return Convert.ToBase64String(bytes);
}
public static string FromBase64String(this string value)
{
if (string.IsNullOrEmpty(value))
{
return "";
}
byte[] bytes = Convert.FromBase64String(value);
return Encoding.UTF8.GetString(bytes);
}
public static string RegexMatch(this string str, string pattern)
{
try
{
return Regex.Match(str, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline).Value;
}
catch
{
return "";
}
}
public static List<string> RegexMatches(this string str, string pattern)
{
List<string> list = new List<string>();
try
{
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
if (regex.IsMatch(str))
{
MatchCollection matchCollection = regex.Matches(str);
foreach (Match match in matchCollection)
{
list.Add(match.Value);
}
}
return list;
}
catch
{
return list;
}
}
public static bool Has(this string str)
{
return !string.IsNullOrEmpty(str?.Trim());
}
public static bool NotHas(this string str)
{
return string.IsNullOrEmpty(str?.Trim());
}
}
}