using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace DH.RBAC.Utility.Other
{
///
/// 字符串操作类
///
public static class StringHelper
{
///
/// 把字符串按照分隔符转换成 List
///
/// 源字符串
/// 分隔符
/// 是否转换为小写
///
public static List SplitToList(this string str, char speater = ',', bool toLower = false)
{
List list = new List();
string[] ss = str.Split(speater);
foreach (string s in ss)
{
if (!string.IsNullOrEmpty(s) && s != speater.ToString())
{
string strVal = s;
if (toLower)
{
strVal = s.ToLower();
}
list.Add(strVal);
}
}
return list;
}
///
/// 把 List 按照分隔符组装成 string
///
///
///
///
public static string GetStrArray(this List list, string speater = ",")
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.Count; i++)
{
if (i == list.Count - 1)
{
sb.Append(list[i]);
}
else
{
sb.Append(list[i]);
sb.Append(speater);
}
}
return sb.ToString();
}
///
/// 删除最后结尾的指定字符后的字符
///
public static string DelLastChar(this string str, string strChar = ",")
{
return str.Substring(0, str.LastIndexOf(strChar));
}
///
/// 转全角的函数(SBC case)
///
///
///
public static string ToSBC(string input)
{
//半角转全角:
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] == 32)
{
c[i] = (char)12288;
continue;
}
if (c[i] < 127)
c[i] = (char)(c[i] + 65248);
}
return new string(c);
}
///
/// 转半角的函数(SBC case)
///
/// 输入
///
public static string ToDBC(string input)
{
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] == 12288)
{
c[i] = (char)32;
continue;
}
if (c[i] > 65280 && c[i] < 65375)
c[i] = (char)(c[i] - 65248);
}
return new string(c);
}
///
/// 获取正确的Id,如果不是正整数,返回0
///
///
/// 返回正确的整数ID,失败返回0
public static int ToInt32(this string value)
{
if (IsNumberId(value))
return int.Parse(value);
else
return 0;
}
///
/// 检查一个字符串是否是纯数字构成的,一般用于查询字符串参数的有效性验证。(0除外)
///
/// 需验证的字符串。。
/// 是否合法的bool值。
public static bool IsNumberId(string _value)
{
return QuickValidate("^[1-9]*[0-9]*$", _value);
}
///
/// 快速验证一个字符串是否符合指定的正则表达式。
///
/// 正则表达式的内容。
/// 需验证的字符串。
/// 是否合法的bool值。
public static bool QuickValidate(string _express, string _value)
{
if (_value == null) return false;
Regex myRegex = new Regex(_express);
if (_value.Length == 0)
{
return false;
}
return myRegex.IsMatch(_value);
}
///
/// 得到字符串长度,一个汉字长度为2
///
/// 参数字符串
///
public static int StrLength(this string inputString)
{
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
int tempLen = 0;
byte[] s = ascii.GetBytes(inputString);
for (int i = 0; i < s.Length; i++)
{
if ((int)s[i] == 63)
tempLen += 2;
else
tempLen += 1;
}
return tempLen;
}
///
/// 截取指定长度字符串
///
/// 要处理的字符串
/// 指定长度
/// 返回处理后的字符串
public static string splitString(this string inputString, int len)
{
bool isShowFix = false;
if (len % 2 == 1)
{
isShowFix = true;
len--;
}
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
int tempLen = 0;
string tempString = "";
byte[] s = ascii.GetBytes(inputString);
for (int i = 0; i < s.Length; i++)
{
if ((int)s[i] == 63)
tempLen += 2;
else
tempLen += 1;
try
{
tempString += inputString.Substring(i, 1);
}
catch
{
break;
}
if (tempLen > len)
break;
}
byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
if (isShowFix && mybyte.Length > len)
tempString += "…";
return tempString;
}
///
/// HTML转行成TEXT
///
///
///
public static string HtmlToTxt(this string strHtml)
{
string[] aryReg ={
@"",
@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
@"([\r\n])[\s]+",
@"&(quot|#34);",
@"&(amp|#38);",
@"&(lt|#60);",
@"&(gt|#62);",
@"&(nbsp|#160);",
@"&(iexcl|#161);",
@"&(cent|#162);",
@"&(pound|#163);",
@"&(copy|#169);",
@"(\d+);",
@"-->",
@"