字符串处理类收集(实例一)

来源:互联网 发布:linux服务器搭建教程 编辑:程序博客网 时间:2024/06/06 05:31
 
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.IO;
  11. using System.Web.SessionState;
  12. using System.Xml;
  13. using System.Security;
  14. using System.Net;
  15. using System.Text.RegularExpressions;
  16. /// <summary>
  17. /// 字符串的处理,字符的转换
  18. /// </summary>
  19. public class StringUtil
  20. {
  21.     public StringUtil()
  22.     {
  23.         //
  24.         // TODO: 在此处添加构造函数逻辑
  25.         //
  26.     }
  27.     /// <summary>
  28.     /// 从字符串中的尾部删除指定的字符串
  29.     /// </summary>
  30.     /// <param name="sourceString"></param>
  31.     /// <param name="removedString"></param>
  32.     /// <returns></returns>
  33.     public static string Remove(string sourceString, string removedString)
  34.     {
  35.         try
  36.         {
  37.             if (sourceString.IndexOf(removedString) < 0)
  38.                 throw new Exception("原字符串中不包含移除字符串!");
  39.             string result = sourceString;
  40.             int lengthOfSourceString = sourceString.Length;
  41.             int lengthOfRemovedString = removedString.Length;
  42.             int startIndex = lengthOfSourceString - lengthOfRemovedString;
  43.             string tempSubString = sourceString.Substring(startIndex);
  44.             if (tempSubString.ToUpper() == removedString.ToUpper())
  45.             {
  46.                 result = sourceString.Remove(startIndex, lengthOfRemovedString);
  47.             }
  48.             return result;
  49.         }
  50.         catch
  51.         {
  52.             return sourceString;
  53.         }
  54.     }
  55.     /// <summary>
  56.     /// 获取拆分符右边的字符串
  57.     /// </summary>
  58.     /// <param name="sourceString"></param>
  59.     /// <param name="splitChar"></param>
  60.     /// <returns></returns>
  61.     public static string RightSplit(string sourceString, char splitChar)
  62.     {
  63.         string result = null;
  64.         string[] tempString = sourceString.Split(splitChar);
  65.         if (tempString.Length > 0)
  66.         {
  67.             result = tempString[tempString.Length - 1].ToString();
  68.         }
  69.         return result;
  70.     }
  71.     /// <summary>
  72.     /// 获取拆分符左边的字符串
  73.     /// </summary>
  74.     /// <param name="sourceString"></param>
  75.     /// <param name="splitChar"></param>
  76.     /// <returns></returns>
  77.     public static string LeftSplit(string sourceString, char splitChar)
  78.     {
  79.         string result = null;
  80.         string[] tempString = sourceString.Split(splitChar);
  81.         if (tempString.Length > 0)
  82.         {
  83.             result = tempString[0].ToString();
  84.         }
  85.         return result;
  86.     }
  87.     /// <summary>
  88.     /// 去掉最后一个逗号
  89.     /// </summary>
  90.     /// <param name="origin"></param>
  91.     /// <returns></returns>
  92.     public static string DelLastComma(string origin)
  93.     {
  94.         if (origin.IndexOf(",") == -1)
  95.         {
  96.             return origin;
  97.         }
  98.         return origin.Substring(0, origin.LastIndexOf(","));
  99.     }
  100.     /// <summary>
  101.     /// 删除不可见字符
  102.     /// </summary>
  103.     /// <param name="sourceString"></param>
  104.     /// <returns></returns>
  105.     public static string DeleteUnVisibleChar(string sourceString)
  106.     {
  107.         System.Text.StringBuilder sBuilder = new System.Text.StringBuilder(131);
  108.         for (int i = 0; i < sourceString.Length; i++)
  109.         {
  110.             int Unicode = sourceString[i];
  111.             if (Unicode >= 16)
  112.             {
  113.                 sBuilder.Append(sourceString[i].ToString());
  114.             }
  115.         }
  116.         return sBuilder.ToString();
  117.     }
  118.     /// <summary>
  119.     /// 获取数组元素的合并字符串
  120.     /// </summary>
  121.     /// <param name="stringArray"></param>
  122.     /// <returns></returns>
  123.     public static string GetArrayString(string[] stringArray)
  124.     {
  125.         string totalString = null;
  126.         for (int i = 0; i < stringArray.Length; i++)
  127.         {
  128.             totalString = totalString + stringArray[i];
  129.         }
  130.         return totalString;
  131.     }
  132.     /// <summary>
  133.     ///     获取某一字符串在字符串数组中出现的次数
  134.     /// </summary>
  135.     /// <param name="stringArray" type="string[]">
  136.     ///     <para>
  137.     ///         
  138.     ///     </para>
  139.     /// </param>
  140.     /// <param name="findString" type="string">
  141.     ///     <para>
  142.     ///         
  143.     ///     </para>
  144.     /// </param>
  145.     /// <returns>
  146.     ///     A int value...
  147.     /// </returns>
  148.     public static int GetStringCount(string[] stringArray, string findString)
  149.     {
  150.         int count = -1;
  151.         string totalString = GetArrayString(stringArray);
  152.         string subString = totalString;
  153.         while (subString.IndexOf(findString) >= 0)
  154.         {
  155.             subString = totalString.Substring(subString.IndexOf(findString));
  156.             count += 1;
  157.         }
  158.         return count;
  159.     }
  160.     /// <summary>
  161.     ///     获取某一字符串在字符串中出现的次数
  162.     /// </summary>
  163.     /// <param name="stringArray" type="string">
  164.     ///     <para>
  165.     ///         原字符串
  166.     ///     </para>
  167.     /// </param>
  168.     /// <param name="findString" type="string">
  169.     ///     <para>
  170.     ///         匹配字符串
  171.     ///     </para>
  172.     /// </param>
  173.     /// <returns>
  174.     ///     匹配字符串数量
  175.     /// </returns>
  176.     public static int GetStringCount(string sourceString, string findString)
  177.     {
  178.         int count = 0;
  179.         int findStringLength = findString.Length;
  180.         string subString = sourceString;
  181.         while (subString.IndexOf(findString) >= 0)
  182.         {
  183.             subString = subString.Substring(subString.IndexOf(findString) + findStringLength);
  184.             count += 1;
  185.         }
  186.         return count;
  187.     }
  188.     /// <summary>
  189.     /// 截取从startString开始到原字符串结尾的所有字符   
  190.     /// </summary>
  191.     /// <param name="sourceString" type="string">
  192.     ///     <para>
  193.     ///         
  194.     ///     </para>
  195.     /// </param>
  196.     /// <param name="startString" type="string">
  197.     ///     <para>
  198.     ///         
  199.     ///     </para>
  200.     /// </param>
  201.     /// <returns>
  202.     ///     A string value...
  203.     /// </returns>
  204.     public static string GetSubString(string sourceString, string startString)
  205.     {
  206.         try
  207.         {
  208.             int index = sourceString.ToUpper().IndexOf(startString);
  209.             if (index > 0)
  210.             {
  211.                 return sourceString.Substring(index);
  212.             }
  213.             return sourceString;
  214.         }
  215.         catch
  216.         {
  217.             return "";
  218.         }
  219.     }
  220.     public static string GetSubString(string sourceString, string beginRemovedString, string endRemovedString)
  221.     {
  222.         try
  223.         {
  224.             if (sourceString.IndexOf(beginRemovedString) != 0)
  225.                 beginRemovedString = "";
  226.             if (sourceString.LastIndexOf(endRemovedString, sourceString.Length - endRemovedString.Length) < 0)
  227.                 endRemovedString = "";
  228.             int startIndex = beginRemovedString.Length;
  229.             int length = sourceString.Length - beginRemovedString.Length - endRemovedString.Length;
  230.             if (length > 0)
  231.             {
  232.                 return sourceString.Substring(startIndex, length);
  233.             }
  234.             return sourceString;
  235.         }
  236.         catch
  237.         {
  238.             return sourceString; ;
  239.         }
  240.     }
  241.     /// <summary>
  242.     /// 按字节数取出字符串的长度
  243.     /// </summary>
  244.     /// <param name="strTmp">要计算的字符串</param>
  245.     /// <returns>字符串的字节数</returns>
  246.     public static int GetByteCount(string strTmp)
  247.     {
  248.         int intCharCount = 0;
  249.         for (int i = 0; i < strTmp.Length; i++)
  250.         {
  251.             if (System.Text.UTF8Encoding.UTF8.GetByteCount(strTmp.Substring(i, 1)) == 3)
  252.             {
  253.                 intCharCount = intCharCount + 2;
  254.             }
  255.             else
  256.             {
  257.                 intCharCount = intCharCount + 1;
  258.             }
  259.         }
  260.         return intCharCount;
  261.     }
  262.     /// <summary>
  263.     /// 按字节数要在字符串的位置
  264.     /// </summary>
  265.     /// <param name="intIns">字符串的位置</param>
  266.     /// <param name="strTmp">要计算的字符串</param>
  267.     /// <returns>字节的位置</returns>
  268.     public static int GetByteIndex(int intIns, string strTmp)
  269.     {
  270.         int intReIns = 0;
  271.         if (strTmp.Trim() == "")
  272.         {
  273.             return intIns;
  274.         }
  275.         for (int i = 0; i < strTmp.Length; i++)
  276.         {
  277.             if (System.Text.UTF8Encoding.UTF8.GetByteCount(strTmp.Substring(i, 1)) == 3)
  278.             {
  279.                 intReIns = intReIns + 2;
  280.             }
  281.             else
  282.             {
  283.                 intReIns = intReIns + 1;
  284.             }
  285.             if (intReIns >= intIns)
  286.             {
  287.                 intReIns = i + 1;
  288.                 break;
  289.             }
  290.         }
  291.         return intReIns;
  292.     }
  293.     /// <summary>
  294.     /// Method to make sure that user's inputs are not malicious
  295.     /// 截取输入最大的字符串
  296.     /// </summary>
  297.     /// <param name="text">User's Input</param>
  298.     /// <param name="maxLength">Maximum length of input</param>
  299.     /// <returns>The cleaned up version of the input</returns>
  300.     public static string InputText(string text, int maxLength)
  301.     {
  302.         text = text.Trim();
  303.         if (string.IsNullOrEmpty(text))
  304.             return string.Empty;
  305.         if (text.Length > maxLength)
  306.             text = text.Substring(0, maxLength);
  307.         text = Regex.Replace(text, "[//s]{2,}"" ");   //two or more spaces
  308.         text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|//n)*?>)""/n");   //<br>
  309.         text = Regex.Replace(text, "(//s*&[n|N][b|B][s|S][p|P];//s*)+"" ");   // 
  310.         text = Regex.Replace(text, "<(.|//n)*?>"string.Empty);    //any other tags
  311.         text = text.Replace("'""''");
  312.         return text;
  313.     }
  314.     /// <summary>
  315.     /// Method to check whether input has other characters than numbers
  316.     /// 清楚字符
  317.     /// </summary>
  318.     public static string CleanNonWord(string text)
  319.     {
  320.         return Regex.Replace(text, "//W""");
  321.     }
  322.     //特殊字符的转换
  323.     public static string Encode(string str)
  324.     {
  325.         str = str.Replace("'""'");
  326.         str = str.Replace("/""""");
  327.         str = str.Replace("<""<");
  328.         str = str.Replace(">"">");
  329.         return str;
  330.     }
  331.     public static string Decode(string str)
  332.     {
  333.         str = str.Replace(">"">");
  334.         str = str.Replace("<""<");
  335.         str = str.Replace(" "" ");
  336.         str = str.Replace(""""/"");
  337.         return str;
  338.     }
  339.     //字符传的转换 用在查询 登陆时 防止恶意的盗取密码
  340.     public static string TBCode(string strtb)
  341.     {
  342.         strtb = strtb.Replace("!""");
  343.         strtb = strtb.Replace("@""");
  344.         strtb = strtb.Replace("#""");
  345.         strtb = strtb.Replace("$""");
  346.         strtb = strtb.Replace("%""");
  347.         strtb = strtb.Replace("^""");
  348.         strtb = strtb.Replace("&""");
  349.         strtb = strtb.Replace("*""");
  350.         strtb = strtb.Replace("(""");
  351.         strtb = strtb.Replace(")""");
  352.         strtb = strtb.Replace("_""");
  353.         strtb = strtb.Replace("+""");
  354.         strtb = strtb.Replace("|""");
  355.         strtb = strtb.Replace("?""");
  356.         strtb = strtb.Replace("/""");
  357.         strtb = strtb.Replace(".""");
  358.         strtb = strtb.Replace(">""");
  359.         strtb = strtb.Replace("<""");
  360.         strtb = strtb.Replace("{""");
  361.         strtb = strtb.Replace("}""");
  362.         strtb = strtb.Replace("[""");
  363.         strtb = strtb.Replace("]""");
  364.         strtb = strtb.Replace("-""");
  365.         strtb = strtb.Replace("=""");
  366.         strtb = strtb.Replace(",""");
  367.         return strtb;
  368.     }
  369.     //以javascript的windous.alert()方法输出提示信息
  370.     //strmsg 表示要输出的信息
  371.     //back 表示输出后是否要history.back()
  372.     //end 表示输出后是否要Response.End()
  373.     public static void WriteMessage(string strMsg, bool Back, bool End)
  374.     {
  375.         string js = "";
  376.        // Response = HttpContext.Current.Response;
  377.         //将单引号替换,防止js出错
  378.         strMsg = strMsg.Replace("'""");
  379.         //将回车符号换掉,防止js出错
  380.         strMsg = strMsg.Replace("/r/n""");
  381.        js="<script language=javascript>alert('" + strMsg + "')</script>";
  382.         if (Back)
  383.         {
  384.             js="<script language=javascript>history.back();</script)";
  385.         }
  386.         if (End)
  387.         {
  388.             js = "<script language=javascript>End</script)";
  389.         }
  390.         HttpContext.Current.Response.Write(js);   
  391.     }
  392.     /// <summary>
  393.     /// 小函数,将字符串转换成float型,若字符串为空,返回0
  394.     /// </summary>
  395.     /// <param name="str">要转换的字符串</param>
  396.     /// <returns>转后的float值</returns>
  397.     public static float GetFloatValue(string str)
  398.     {
  399.         try
  400.         {
  401.             if (str.Length == 0)
  402.                 return 0;
  403.             else
  404.                 return float.Parse(str);
  405.         }
  406.         catch
  407.         {
  408.             WriteMessage("数据转换失败,请检查数据是否合理!"truetrue);
  409.             return 0;
  410.         }
  411.     }
  412.     /// <summary>
  413.     /// 小函数,将字符串转换成int型,若字符串为空,返回0
  414.     /// </summary>
  415.     /// <param name="str">要转换的字符串</param>
  416.     /// <returns>转后的int值</returns>
  417.     public static int GetIntValue(string str)
  418.     {
  419.         try
  420.         {
  421.             if (str.Length == 0)
  422.                 return 0;
  423.             else
  424.             {
  425.                 if (Int32.Parse(str) == 0)
  426.                     return 0;
  427.                 else
  428.                     return Int32.Parse(str);
  429.             }
  430.         }
  431.         catch
  432.         {
  433.             WriteMessage("数据转换失败,请检查数据是否合理!"truetrue);
  434.             return 0;
  435.         }
  436.     }
  437. }
原创粉丝点击