매우 실 용적 인 C\#문자열 작업 처리 클래스 StringHelper.cs
19528 단어 C#문자열 처리 클래스
/// <summary>
/// :Assistant
/// :
/// :361983679
/// :http://www.sufeinet.com/thread-655-1-1.html
/// </summary>
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace DotNet.Utilities
{
/// <summary>
///
/// 1、GetStrArray(string str, char speater, bool toLower) List
/// 2、GetStrArray(string str) ,
/// 3、GetArrayStr(List list, string speater) List string
/// 4、GetArrayStr(List list)
/// 5、GetArrayValueStr(Dictionary<int, int> list)
/// 6、DelLastComma(string str)
/// 7、DelLastChar(string str, string strchar)
/// 8、ToSBC(string input) (SBC case)
/// 9、ToDBC(string input) (SBC case)
/// 10、GetSubStringList(string o_str, char sepeater) List
/// 11、GetCleanStyle(string StrList, string SplitString)
/// 12、GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error)
/// 13、SplitMulti(string str, string splitstr)
/// 14、SqlSafeString(string String, bool IsDel)
/// </summary>
public class StringHelper
{
/// <summary>
/// List
/// </summary>
/// <param name="str"> </param>
/// <param name="speater"> </param>
/// <param name="toLower"> </param>
/// <returns></returns>
public static List<string> GetStrArray(string str, char speater, bool toLower)
{
List<string> list = new List<string>();
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;
}
/// <summary>
/// ,
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string[] GetStrArray(string str)
{
return str.Split(new Char[] { ',' });
}
/// <summary>
/// List<string> string
/// </summary>
/// <param name="list"></param>
/// <param name="speater"></param>
/// <returns></returns>
public static string GetArrayStr(List<string> 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();
}
/// <summary>
///
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static string GetArrayStr(List<int> list)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.Count; i++)
{
if (i == list.Count - 1)
{
sb.Append(list[i].ToString());
}
else
{
sb.Append(list[i]);
sb.Append(",");
}
}
return sb.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static string GetArrayValueStr(Dictionary<int, int> list)
{
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<int, int> kvp in list)
{
sb.Append(kvp.Value + ",");
}
if (list.Count > 0)
{
return DelLastComma(sb.ToString());
}
else
{
return "";
}
}
#region
/// <summary>
///
/// </summary>
public static string DelLastComma(string str)
{
return str.Substring(0, str.LastIndexOf(","));
}
/// <summary>
///
/// </summary>
public static string DelLastChar(string str, string strchar)
{
return str.Substring(0, str.LastIndexOf(strchar));
}
#endregion
/// <summary>
/// (SBC case)
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
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);
}
/// <summary>
/// (SBC case)
/// </summary>
/// <param name="input"> </param>
/// <returns></returns>
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);
}
/// <summary>
/// List
/// </summary>
/// <param name="o_str"></param>
/// <param name="sepeater"></param>
/// <returns></returns>
public static List<string> GetSubStringList(string o_str, char sepeater)
{
List<string> list = new List<string>();
string[] ss = o_str.Split(sepeater);
foreach (string s in ss)
{
if (!string.IsNullOrEmpty(s) && s != sepeater.ToString())
{
list.Add(s);
}
}
return list;
}
#region
/// <summary>
///
/// </summary>
/// <param name="StrList"></param>
/// <param name="SplitString"></param>
/// <returns></returns>
public static string GetCleanStyle(string StrList, string SplitString)
{
string RetrunValue = "";
// ,
if (StrList == null)
{
RetrunValue = "";
}
else
{
//
string NewString = "";
NewString = StrList.Replace(SplitString, "");
RetrunValue = NewString;
}
return RetrunValue;
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <param name="StrList"></param>
/// <param name="NewStyle"></param>
/// <param name="SplitString"></param>
/// <param name="Error"></param>
/// <returns></returns>
public static string GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error)
{
string ReturnValue = "";
// , ,
if (StrList == null)
{
ReturnValue = "";
Error = " ";
}
else
{
// , , 。
int strListLength = StrList.Length;
int NewStyleLength = GetCleanStyle(NewStyle, SplitString).Length;
if (strListLength != NewStyleLength)
{
ReturnValue = "";
Error = " , ";
}
else
{
//
string Lengstr = "";
for (int i = 0; i < NewStyle.Length; i++)
{
if (NewStyle.Substring(i, 1) == SplitString)
{
Lengstr = Lengstr + "," + i;
}
}
if (Lengstr != "")
{
Lengstr = Lengstr.Substring(1);
}
//
string[] str = Lengstr.Split(',');
foreach (string bb in str)
{
StrList = StrList.Insert(int.Parse(bb), SplitString);
}
//
ReturnValue = StrList;
// ,
Error = "";
}
}
return ReturnValue;
}
#endregion
/// <summary>
///
/// </summary>
/// <param name="str"></param>
/// <param name="splitstr"></param>
/// <returns></returns>
public static string[] SplitMulti(string str, string splitstr)
{
string[] strArray = null;
if ((str != null) && (str != ""))
{
strArray = new Regex(splitstr).Split(str);
}
return strArray;
}
public static string SqlSafeString(string String, bool IsDel)
{
if (IsDel)
{
String = String.Replace("'", "");
String = String.Replace("\"", "");
return String;
}
String = String.Replace("'", "'");
String = String.Replace("\"", """);
return String;
}
#region Id, , 0
/// <summary>
/// Id, , 0
/// </summary>
/// <param name="_value"></param>
/// <returns> ID, 0</returns>
public static int StrToId(string _value)
{
if (IsNumberId(_value))
return int.Parse(_value);
else
return 0;
}
#endregion
#region , 。
/// <summary>
/// , 。(0 )
/// </summary>
/// <param name="_value"> 。。</param>
/// <returns> bool 。</returns>
public static bool IsNumberId(string _value)
{
return QuickValidate("^[1-9]*[0-9]*$", _value);
}
#endregion
#region 。
/// <summary>
/// 。
/// </summary>
/// <param name="_express"> 。</param>
/// <param name="_value"> 。</param>
/// <returns> bool 。</returns>
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);
}
#endregion
#region MD5
/// <summary>
/// MD5
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string GetMD5(string s)
{
//md5
s = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s, "md5").ToString();
return s.ToLower().Substring(8, 16);
}
#endregion
#region , 2
/// <summary>
/// , 2
/// </summary>
/// <param name="inputString"> </param>
/// <returns></returns>
public static int StrLength(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;
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <param name="inputString"> </param>
/// <param name="len"> </param>
/// <returns> </returns>
public static string ClipString(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;
}
#endregion
#region HTML TEXT
/// <summary>
/// HTML TEXT
/// </summary>
/// <param name="strHtml"></param>
/// <returns></returns>
public static string HtmlToTxt(string strHtml)
{
string[] aryReg ={
@"<script[^>]*?>.*?</script>",
@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
@"([\r
])[\s]+",
@"&(quot|#34);",
@"&(amp|#38);",
@"&(lt|#60);",
@"&(gt|#62);",
@"&(nbsp|#160);",
@"&(iexcl|#161);",
@"&(cent|#162);",
@"&(pound|#163);",
@"&(copy|#169);",
@"&#(\d+);",
@"-->",
@"<!--.*
"
};
string newReg = aryReg[0];
string strOutput = strHtml;
for (int i = 0; i < aryReg.Length; i++)
{
Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
strOutput = regex.Replace(strOutput, string.Empty);
}
strOutput.Replace("<", "");
strOutput.Replace(">", "");
strOutput.Replace("\r
", "");
return strOutput;
}
#endregion
#region
/// <summary>
/// , true
/// </summary>
/// <typeparam name="T"> </typeparam>
/// <param name="data"> </param>
public static bool IsNullOrEmpty<T>(T data)
{
// null
if (data == null)
{
return true;
}
// ""
if (data.GetType() == typeof(String))
{
if (string.IsNullOrEmpty(data.ToString().Trim()))
{
return true;
}
}
// DBNull
if (data.GetType() == typeof(DBNull))
{
return true;
}
//
return false;
}
/// <summary>
/// , true
/// </summary>
/// <param name="data"> </param>
public static bool IsNullOrEmpty(object data)
{
// null
if (data == null)
{
return true;
}
// ""
if (data.GetType() == typeof(String))
{
if (string.IsNullOrEmpty(data.ToString().Trim()))
{
return true;
}
}
// DBNull
if (data.GetType() == typeof(DBNull))
{
return true;
}
//
return false;
}
#endregion
}
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
WebView2를 Visual Studio 2017 Express에서 사용할 수 있을 때까지Evergreen .Net Framework SDK 4.8 VisualStudio2017에서 NuGet을 사용하기 때문에 패키지 관리 방법을 packages.config 대신 PackageReference를 사용해야...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.