字符串处理类收集(实例一)
来源:互联网 发布:linux服务器搭建教程 编辑:程序博客网 时间:2024/06/06 05:31
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.IO;
- using System.Web.SessionState;
- using System.Xml;
- using System.Security;
- using System.Net;
- using System.Text.RegularExpressions;
- public class StringUtil
- {
- public StringUtil()
- {
-
-
-
- }
-
-
-
-
-
-
- public static string Remove(string sourceString, string removedString)
- {
- try
- {
- if (sourceString.IndexOf(removedString) < 0)
- throw new Exception("原字符串中不包含移除字符串!");
- string result = sourceString;
- int lengthOfSourceString = sourceString.Length;
- int lengthOfRemovedString = removedString.Length;
- int startIndex = lengthOfSourceString - lengthOfRemovedString;
- string tempSubString = sourceString.Substring(startIndex);
- if (tempSubString.ToUpper() == removedString.ToUpper())
- {
- result = sourceString.Remove(startIndex, lengthOfRemovedString);
- }
- return result;
- }
- catch
- {
- return sourceString;
- }
- }
-
-
-
-
-
-
- public static string RightSplit(string sourceString, char splitChar)
- {
- string result = null;
- string[] tempString = sourceString.Split(splitChar);
- if (tempString.Length > 0)
- {
- result = tempString[tempString.Length - 1].ToString();
- }
- return result;
- }
-
-
-
-
-
-
- public static string LeftSplit(string sourceString, char splitChar)
- {
- string result = null;
- string[] tempString = sourceString.Split(splitChar);
- if (tempString.Length > 0)
- {
- result = tempString[0].ToString();
- }
- return result;
- }
-
-
-
-
-
- public static string DelLastComma(string origin)
- {
- if (origin.IndexOf(",") == -1)
- {
- return origin;
- }
- return origin.Substring(0, origin.LastIndexOf(","));
- }
-
-
-
-
-
- public static string DeleteUnVisibleChar(string sourceString)
- {
- System.Text.StringBuilder sBuilder = new System.Text.StringBuilder(131);
- for (int i = 0; i < sourceString.Length; i++)
- {
- int Unicode = sourceString[i];
- if (Unicode >= 16)
- {
- sBuilder.Append(sourceString[i].ToString());
- }
- }
- return sBuilder.ToString();
- }
-
-
-
-
-
- public static string GetArrayString(string[] stringArray)
- {
- string totalString = null;
- for (int i = 0; i < stringArray.Length; i++)
- {
- totalString = totalString + stringArray[i];
- }
- return totalString;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static int GetStringCount(string[] stringArray, string findString)
- {
- int count = -1;
- string totalString = GetArrayString(stringArray);
- string subString = totalString;
- while (subString.IndexOf(findString) >= 0)
- {
- subString = totalString.Substring(subString.IndexOf(findString));
- count += 1;
- }
- return count;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static int GetStringCount(string sourceString, string findString)
- {
- int count = 0;
- int findStringLength = findString.Length;
- string subString = sourceString;
- while (subString.IndexOf(findString) >= 0)
- {
- subString = subString.Substring(subString.IndexOf(findString) + findStringLength);
- count += 1;
- }
- return count;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static string GetSubString(string sourceString, string startString)
- {
- try
- {
- int index = sourceString.ToUpper().IndexOf(startString);
- if (index > 0)
- {
- return sourceString.Substring(index);
- }
- return sourceString;
- }
- catch
- {
- return "";
- }
- }
- public static string GetSubString(string sourceString, string beginRemovedString, string endRemovedString)
- {
- try
- {
- if (sourceString.IndexOf(beginRemovedString) != 0)
- beginRemovedString = "";
- if (sourceString.LastIndexOf(endRemovedString, sourceString.Length - endRemovedString.Length) < 0)
- endRemovedString = "";
- int startIndex = beginRemovedString.Length;
- int length = sourceString.Length - beginRemovedString.Length - endRemovedString.Length;
- if (length > 0)
- {
- return sourceString.Substring(startIndex, length);
- }
- return sourceString;
- }
- catch
- {
- return sourceString; ;
- }
- }
-
-
-
-
-
- public static int GetByteCount(string strTmp)
- {
- int intCharCount = 0;
- for (int i = 0; i < strTmp.Length; i++)
- {
- if (System.Text.UTF8Encoding.UTF8.GetByteCount(strTmp.Substring(i, 1)) == 3)
- {
- intCharCount = intCharCount + 2;
- }
- else
- {
- intCharCount = intCharCount + 1;
- }
- }
- return intCharCount;
- }
-
-
-
-
-
-
- public static int GetByteIndex(int intIns, string strTmp)
- {
- int intReIns = 0;
- if (strTmp.Trim() == "")
- {
- return intIns;
- }
- for (int i = 0; i < strTmp.Length; i++)
- {
- if (System.Text.UTF8Encoding.UTF8.GetByteCount(strTmp.Substring(i, 1)) == 3)
- {
- intReIns = intReIns + 2;
- }
- else
- {
- intReIns = intReIns + 1;
- }
- if (intReIns >= intIns)
- {
- intReIns = i + 1;
- break;
- }
- }
- return intReIns;
- }
-
-
-
-
-
-
-
- public static string InputText(string text, int maxLength)
- {
- text = text.Trim();
- if (string.IsNullOrEmpty(text))
- return string.Empty;
- if (text.Length > maxLength)
- text = text.Substring(0, maxLength);
- text = Regex.Replace(text, "[//s]{2,}", " ");
- text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|//n)*?>)", "/n");
- text = Regex.Replace(text, "(//s*&[n|N][b|B][s|S][p|P];//s*)+", " ");
- text = Regex.Replace(text, "<(.|//n)*?>", string.Empty);
- text = text.Replace("'", "''");
- return text;
- }
-
-
-
-
- public static string CleanNonWord(string text)
- {
- return Regex.Replace(text, "//W", "");
- }
-
- public static string Encode(string str)
- {
- str = str.Replace("'", "'");
- str = str.Replace("/"", """);
- str = str.Replace("<", "<");
- str = str.Replace(">", ">");
- return str;
- }
- public static string Decode(string str)
- {
- str = str.Replace(">", ">");
- str = str.Replace("<", "<");
- str = str.Replace(" ", " ");
- str = str.Replace(""", "/"");
- return str;
- }
-
- public static string TBCode(string strtb)
- {
- strtb = strtb.Replace("!", "");
- strtb = strtb.Replace("@", "");
- strtb = strtb.Replace("#", "");
- strtb = strtb.Replace("$", "");
- strtb = strtb.Replace("%", "");
- strtb = strtb.Replace("^", "");
- strtb = strtb.Replace("&", "");
- strtb = strtb.Replace("*", "");
- strtb = strtb.Replace("(", "");
- strtb = strtb.Replace(")", "");
- strtb = strtb.Replace("_", "");
- strtb = strtb.Replace("+", "");
- strtb = strtb.Replace("|", "");
- strtb = strtb.Replace("?", "");
- strtb = strtb.Replace("/", "");
- strtb = strtb.Replace(".", "");
- strtb = strtb.Replace(">", "");
- strtb = strtb.Replace("<", "");
- strtb = strtb.Replace("{", "");
- strtb = strtb.Replace("}", "");
- strtb = strtb.Replace("[", "");
- strtb = strtb.Replace("]", "");
- strtb = strtb.Replace("-", "");
- strtb = strtb.Replace("=", "");
- strtb = strtb.Replace(",", "");
- return strtb;
- }
-
-
-
-
- public static void WriteMessage(string strMsg, bool Back, bool End)
- {
- string js = "";
-
-
- strMsg = strMsg.Replace("'", "");
-
- strMsg = strMsg.Replace("/r/n", "");
- js="<script language=javascript>alert('" + strMsg + "')</script>";
- if (Back)
- {
- js="<script language=javascript>history.back();</script)";
- }
- if (End)
- {
- js = "<script language=javascript>End</script)";
- }
- HttpContext.Current.Response.Write(js);
- }
-
-
-
-
-
- public static float GetFloatValue(string str)
- {
- try
- {
- if (str.Length == 0)
- return 0;
- else
- return float.Parse(str);
- }
- catch
- {
- WriteMessage("数据转换失败,请检查数据是否合理!", true, true);
- return 0;
- }
- }
-
-
-
-
-
- public static int GetIntValue(string str)
- {
- try
- {
- if (str.Length == 0)
- return 0;
- else
- {
- if (Int32.Parse(str) == 0)
- return 0;
- else
- return Int32.Parse(str);
- }
- }
- catch
- {
- WriteMessage("数据转换失败,请检查数据是否合理!", true, true);
- return 0;
- }
- }
- }