分享通用基类库-C#通用字符串处理类

原创 2012年03月30日 13:40:53
/*************************************************************************************
* 代码:吴蒋
* 时间:2012.03.30
* 说明:字符串处理公共基类
* 其他:
* 修改人:
* 修改时间:
* 修改说明:
************************************************************************************/
using System;
using System.Web;
using System.Text.RegularExpressions;
using System.Text;
namespace Common
{
    public class StringHelp
    {
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public StringHelp() { }
        /// <summary>
        /// 判断参数字符是否为空
        /// </summary>
        /// <param name="sourceString">输入字符串</param>
        /// <returns>True:为空;falsh:不为空</returns>
        public static bool IsNull(string sourceString)
        {
            if (String.Equals(sourceString, null) || String.Equals(sourceString, ""))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 获取操作字符串长度
        /// </summary>
        /// <param name="sourceString">输入字符串</param>
        /// <returns>返回长度</returns>
        public static int MaxLength(string sourceString)
        {
            return sourceString.Length;
        }
        /// <summary>
        /// 双向截取一定长度字符串
        /// </summary>
        /// <param name="sourceString">输入操作字符串</param>
        /// <param name="leftOrRight">截取方向True:Left;Flash:Right</param>
        /// <param name="size">要截取的大小</param>
        /// <returns>返回截取后的字符串</returns>
        public static string InterceptString(string sourceString, bool leftOrRight, int size)
        {
            if (IsNull(sourceString))
            { throw new Exception("操作字符串参数不能为空!"); }
            int maxLength = MaxLength(sourceString);
            if (size >= maxLength)
            {
                return sourceString;
            }
            if (leftOrRight)
            {
                return sourceString.Substring(0, size);
            }
            return sourceString.Substring(maxLength - size, size);
        }
        /// <summary>
        /// 浅去除Html
        /// </summary>
        /// <param name="sourceString">输入操作字符串</param>
        /// <returns>返回操作后字符串</returns>
        public static string WipeOffHtml(string sourceString)
        {
            return Regex.Replace(sourceString, "<[^>]*>", "");
        }
        /// <summary>
        /// 深去除Html包括脚本等
        /// </summary>
        /// <param name="sourceString">输入操作字符串</param>
        /// <returns>返回操作后字符串</returns>
        public string ScriptHtml(string sourceString)
        {
            string[] aryReg = {
             @"<script[^>]*?>.*?</script>",
             @"<(\/\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+);",
             @"-->",
             @"<!--.*\n"
         };
            string[] aryRep = {
             "",
             "",
             "",
             "\"",
             "&",
             "<",
             ">",
             " ",
             "\xa1",//chr(161),
             "\xa2",//chr(162),
             "\xa3",//chr(163),
             "\xa9",//chr(169),
             "",
             "\r\n",
             ""
         };
            string newReg = aryReg[0];
            string strOutput = sourceString;
            for (int i = 0; i < aryReg.Length; i++)
            {
                Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
                strOutput = regex.Replace(strOutput, aryRep[i]);
            }
            strOutput.Replace("<", "");
            strOutput.Replace(">", "");
            strOutput.Replace("\r\n", "");
            return strOutput;
        }
        /// <summary>
        /// 输入HTML中的ImgUrl
        /// </summary>
        /// <param name="sourceString">输入操作字符串</param>
        /// <returns>返回干净的Url</returns>
        public static string GetImgUrl(string sourceString)
        {
            string str = string.Empty;
            Regex r = new Regex(@"<img\s+[^>]*\s*src\s*=\s*([']?)(?<url>\S+)'?[^>]*>",
            RegexOptions.Compiled);
            Match m = r.Match(sourceString.ToLower());
            if (m.Success)
                str = m.Result("${url}");
            return str;
        }
        /// <summary>
        /// 过滤SQL注入
        /// </summary>
        /// <param name="sourceString">输入操作字符串</param>
        /// <returns>返回安全的SQL</returns>
        public static string FilterSql(string sourceString)
        {
            string str = sourceString;
            str = str.Replace("'", "''");
            str = str.Replace("<", "<");
            str = str.Replace(">", ">");
            return str;
        }
        /// <summary>
        /// 移除非法或不友好字符
        /// </summary>
        /// <param name="keyWord">非法或不友好字符以|隔开</param>
        /// <param name="chkStr">要处理的字符串</param>
        /// <returns>处理后的字符串</returns>
        public static string FilterBadWords(string keyWord, string sourceString)
        {
            if (sourceString == "")
            {
                return "";
            }
            string[] bwords = keyWord.Split('|');
            int i, j;
            string str;
            StringBuilder sb = new StringBuilder();
            for (i = 0; i < bwords.Length; i++)
            {
                str = bwords[i].ToString().Trim();
                string regStr, toStr;
                regStr = str;
                Regex r = new Regex(regStr, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline);
                Match m = r.Match(sourceString);
                if (m.Success)
                {
                    j = m.Value.Length;
                    sb.Insert(0, "*", j);
                    toStr = sb.ToString();
                    sourceString = Regex.Replace(sourceString, regStr, toStr, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline);
                }
                sb.Remove(0, sb.Length);
            }
            return sourceString;
        }
        /// <summary>
        /// 判断是否是合法IPV4
        /// </summary>
        /// <param name="sourceString">要处理的字符串</param>
        /// <returns>返回结果</returns>
        public static bool IsIPV4(string sourceString)
        {
            Regex rx = new Regex(@"((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))");
            return (rx.IsMatch(sourceString));
        }
        /// <summary>
        /// IPV4转数字
        /// </summary>
        /// <param name="sourceString">要处理的字符串</param>
        /// <returns>返回结果</returns>
        public static double IPV4Convert(string sourceString)
        {
            if (!IsIPV4(sourceString))
            {
                throw new Exception("IP不合法!");
            }
            string[] listIP = sourceString.Split('.');
            StringBuilder intIP = new StringBuilder();
            for (int i = 0; i < listIP.Length; i++)
            {
                string ipPart = listIP[i].ToString();
               
                if (ipPart.Length == 3)
                {
                    intIP.Append(ipPart);
                }
                if (ipPart.Length == 2)
                {
                    intIP.Append("0" + ipPart);
                }
                if (ipPart.Length == 1)
                {
                    intIP.Append("00" + ipPart);
                }
            }
            return Convert.ToDouble(intIP.ToString());
        }
        /// <summary>
        /// 正则表达式判断字符串格式
        /// </summary>
        /// <param name="sourceString">要处理的字符串</param>
        /// <param name="strVerdict">正则表达式</param>
        /// <returns>返回结果</returns>
        public static bool FormatVerdict(string sourceString, string strVerdict)
        {
            Regex rx = new Regex(strVerdict);
            return (rx.IsMatch(sourceString));
        }
        /// <summary>
        /// 从字符串中的尾部删除指定的字符串
        /// </summary>
        /// <param name="sourceString">需要处理的字符串</param>
        /// <param name="removedString">删除匹配字符串</param>
        /// <returns>处理后字符串</returns>
        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;
            }
        }
        /// <summary>
        /// 获取拆分符右边的字符串
        /// </summary>
        /// <param name="sourceString"></param>
        /// <param name="splitChar"></param>
        /// <returns></returns>
        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;
        }
        /// <summary>
        /// 获取拆分符左边的字符串
        /// </summary>
        /// <param name="sourceString"></param>
        /// <param name="splitChar"></param>
        /// <returns></returns>
        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;
        }
        /// <summary>
        /// 去掉最后一个逗号
        /// </summary>
        /// <param name="origin"></param>
        /// <returns></returns>
        public static string DelLastComma(string origin)
        {
            if (origin.IndexOf(",") == -1)
            {
                return origin;
            }
            return origin.Substring(0, origin.LastIndexOf(","));
        }
        /// <summary>
        /// 删除不可见字符
        /// </summary>
        /// <param name="sourceString"></param>
        /// <returns></returns>
        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();
        }
        /// <summary>
        /// 获取数组元素的合并字符串
        /// </summary>
        /// <param name="stringArray"></param>
        /// <returns></returns>
        public static string GetArrayString(string[] stringArray)
        {
            string totalString = null;
            for (int i = 0; i < stringArray.Length; i++)
            {
                totalString = totalString + stringArray[i];
            }
            return totalString;
        }
        /// <summary>
        /// 获取某一字符串在字符串数组中出现的次数
        /// </summary>
        /// <param name="stringArray">输入要查询的字符串</param>
        /// <param name="findString">输入操作字符串</param>
        /// <returns>返回出现次数</returns>
        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;
        }
        /// <summary>
        /// 获取某一字符串在字符串中出现的次数
        /// </summary>
        /// <param name="sourceString">输入操作字符串</param>
        /// <param name="findString">要查询的字符</param>
        /// <returns>返回出现次数</returns>
        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;
        }
        /// <summary>
        /// 截取从startString开始到原字符串结尾的所有字符
        /// </summary>
        /// <param name="sourceString">需要操作的字符串</param>
        /// <param name="startString">开始字符</param>
        /// <returns>返回结果</returns>
        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 "";
            }
        }
        /// <summary>
        /// 删除从beginRemovedString到 endRemovedString的字符串
        /// </summary>
        /// <param name="sourceString">需要操作的字符串</param>
        /// <param name="beginRemovedString">开始字符</param>
        /// <param name="endRemovedString">结束字符</param>
        /// <returns>返回结果</returns>
        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; ;
            }
        }
        /// <summary>
        /// 按字节数取出字符串的长度
        /// </summary>
        /// <param name="strTmp">要计算的字符串</param>
        /// <returns>字符串的字节数</returns>
        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;
        }
        /// <summary>
        /// 按字节数要在字符串的位置
        /// </summary>
        /// <param name="intIns">字符串的位置</param>
        /// <param name="strTmp">要计算的字符串</param>
        /// <returns>字节的位置</returns>
        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;
        }
        /// <summary>
        /// 截取输入最大的字符串
        /// </summary>
        /// <param name="text">输入字符串</param>
        /// <param name="maxLength">最大长度</param>
        /// <returns>返回操作结果</returns>
        public static string InputText(string sourceString, int maxLength)
        {
            sourceString = sourceString.Trim();
            if (string.IsNullOrEmpty(sourceString))
                return string.Empty;
            if (sourceString.Length > maxLength)
                sourceString = sourceString.Substring(0, maxLength);
            sourceString = Regex.Replace(sourceString, "[\\s]{2,}", " "); //two or more spaces
            sourceString = Regex.Replace(sourceString, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br>
            sourceString = Regex.Replace(sourceString, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); //
            sourceString = Regex.Replace(sourceString, "<(.|\\n)*?>", string.Empty); //any other tags
            sourceString = sourceString.Replace("'", "''");
            return sourceString;
        }
        /// <summary>
        /// 转义操作
        /// </summary>
        /// <param name="sourceString">输入操作字符串</param>
        /// <returns>返回操作结果</returns>
        public static string Encode(string sourceString)
        {
            sourceString = sourceString.Replace("'", "'");
            sourceString = sourceString.Replace("\"", """);
            sourceString = sourceString.Replace("<", "<");
            sourceString = sourceString.Replace(">", ">");
            return sourceString;
        }
        /// <summary>
        /// 反义操作
        /// </summary>
        /// <param name="sourceString">输入操作字符串</param>
        /// <returns>返回操作结果</returns>
        public static string Decode(string sourceString)
        {
            sourceString = sourceString.Replace(">", ">");
            sourceString = sourceString.Replace("<", "<");
            sourceString = sourceString.Replace(" ", " ");
            sourceString = sourceString.Replace(""", "\"");
            return sourceString;
        }
        /// <summary>
        /// 字符传的转换 用在查询 登陆时 防止恶意的盗取密码
        /// </summary>
        /// <param name="sourceString">输入操作字符串</param>
        /// <returns>返回操作结果</returns>
        public static string TBCode(string sourceString)
        {
            sourceString = sourceString.Replace("!", "");
            sourceString = sourceString.Replace("@", "");
            sourceString = sourceString.Replace("#", "");
            sourceString = sourceString.Replace("$", "");
            sourceString = sourceString.Replace("%", "");
            sourceString = sourceString.Replace("^", "");
            sourceString = sourceString.Replace("&", "");
            sourceString = sourceString.Replace("*", "");
            sourceString = sourceString.Replace("(", "");
            sourceString = sourceString.Replace(")", "");
            sourceString = sourceString.Replace("_", "");
            sourceString = sourceString.Replace("+", "");
            sourceString = sourceString.Replace("|", "");
            sourceString = sourceString.Replace("?", "");
            sourceString = sourceString.Replace("/", "");
            sourceString = sourceString.Replace(".", "");
            sourceString = sourceString.Replace(">", "");
            sourceString = sourceString.Replace("<", "");
            sourceString = sourceString.Replace("{", "");
            sourceString = sourceString.Replace("}", "");
            sourceString = sourceString.Replace("[", "");
            sourceString = sourceString.Replace("]", "");
            sourceString = sourceString.Replace("-", "");
            sourceString = sourceString.Replace("=", "");
            sourceString = sourceString.Replace(",", "");
            return sourceString;
        }
    }
}
/*************************************************************************************
* 代码:吴蒋
* 时间:2012.03.30
* 说明:字符串处理公共基类
* 其他:
* 修改人:
* 修改时间:
* 修改说明:
************************************************************************************/
using System;
using System.Web;
using System.Text.RegularExpressions;
using System.Text;
namespace Common
{
    public class StringHelp
    {
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public StringHelp() { }
        /// <summary>
        /// 判断参数字符是否为空
        /// </summary>
        /// <param name="sourceString">输入字符串</param>
        /// <returns>True:为空;falsh:不为空</returns>
        public static bool IsNull(string sourceString)
        {
            if (String.Equals(sourceString, null) || String.Equals(sourceString, ""))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 获取操作字符串长度
        /// </summary>
        /// <param name="sourceString">输入字符串</param>
        /// <returns>返回长度</returns>
        public static int MaxLength(string sourceString)
        {
            return sourceString.Length;
        }
        /// <summary>
        /// 双向截取一定长度字符串
        /// </summary>
        /// <param name="sourceString">输入操作字符串</param>
        /// <param name="leftOrRight">截取方向True:Left;Flash:Right</param>
        /// <param name="size">要截取的大小</param>
        /// <returns>返回截取后的字符串</returns>
        public static string InterceptString(string sourceString, bool leftOrRight, int size)
        {
            if (IsNull(sourceString))
            { throw new Exception("操作字符串参数不能为空!"); }
            int maxLength = MaxLength(sourceString);
            if (size >= maxLength)
            {
                return sourceString;
            }
            if (leftOrRight)
            {
                return sourceString.Substring(0, size);
            }
            return sourceString.Substring(maxLength - size, size);
        }
        /// <summary>
        /// 浅去除Html
        /// </summary>
        /// <param name="sourceString">输入操作字符串</param>
        /// <returns>返回操作后字符串</returns>
        public static string WipeOffHtml(string sourceString)
        {
            return Regex.Replace(sourceString, "<[^>]*>", "");
        }
        /// <summary>
        /// 深去除Html包括脚本等
        /// </summary>
        /// <param name="sourceString">输入操作字符串</param>
        /// <returns>返回操作后字符串</returns>
        public string ScriptHtml(string sourceString)
        {
            string[] aryReg = {
             @"<script[^>]*?>.*?</script>",
             @"<(\/\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+);",
             @"-->",
             @"<!--.*\n"
         };
            string[] aryRep = {
             "",
             "",
             "",
             "\"",
             "&",
             "<",
             ">",
             " ",
             "\xa1",//chr(161),
             "\xa2",//chr(162),
             "\xa3",//chr(163),
             "\xa9",//chr(169),
             "",
             "\r\n",
             ""
         };
            string newReg = aryReg[0];
            string strOutput = sourceString;
            for (int i = 0; i < aryReg.Length; i++)
            {
                Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
                strOutput = regex.Replace(strOutput, aryRep[i]);
            }
            strOutput.Replace("<", "");
            strOutput.Replace(">", "");
            strOutput.Replace("\r\n", "");
            return strOutput;
        }
        /// <summary>
        /// 输入HTML中的ImgUrl
        /// </summary>
        /// <param name="sourceString">输入操作字符串</param>
        /// <returns>返回干净的Url</returns>
        public static string GetImgUrl(string sourceString)
        {
            string str = string.Empty;
            Regex r = new Regex(@"<img\s+[^>]*\s*src\s*=\s*([']?)(?<url>\S+)'?[^>]*>",
            RegexOptions.Compiled);
            Match m = r.Match(sourceString.ToLower());
            if (m.Success)
                str = m.Result("${url}");
            return str;
        }
        /// <summary>
        /// 过滤SQL注入
        /// </summary>
        /// <param name="sourceString">输入操作字符串</param>
        /// <returns>返回安全的SQL</returns>
        public static string FilterSql(string sourceString)
        {
            string str = sourceString;
            str = str.Replace("'", "''");
            str = str.Replace("<", "<");
            str = str.Replace(">", ">");
            return str;
        }
        /// <summary>
        /// 移除非法或不友好字符
        /// </summary>
        /// <param name="keyWord">非法或不友好字符以|隔开</param>
        /// <param name="chkStr">要处理的字符串</param>
        /// <returns>处理后的字符串</returns>
        public static string FilterBadWords(string keyWord, string sourceString)
        {
            if (sourceString == "")
            {
                return "";
            }
            string[] bwords = keyWord.Split('|');
            int i, j;
            string str;
            StringBuilder sb = new StringBuilder();
            for (i = 0; i < bwords.Length; i++)
            {
                str = bwords[i].ToString().Trim();
                string regStr, toStr;
                regStr = str;
                Regex r = new Regex(regStr, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline);
                Match m = r.Match(sourceString);
                if (m.Success)
                {
                    j = m.Value.Length;
                    sb.Insert(0, "*", j);
                    toStr = sb.ToString();
                    sourceString = Regex.Replace(sourceString, regStr, toStr, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline);
                }
                sb.Remove(0, sb.Length);
            }
            return sourceString;
        }
        /// <summary>
        /// 判断是否是合法IPV4
        /// </summary>
        /// <param name="sourceString">要处理的字符串</param>
        /// <returns>返回结果</returns>
        public static bool IsIPV4(string sourceString)
        {
            Regex rx = new Regex(@"((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))");
            return (rx.IsMatch(sourceString));
        }
        /// <summary>
        /// IPV4转数字
        /// </summary>
        /// <param name="sourceString">要处理的字符串</param>
        /// <returns>返回结果</returns>
        public static double IPV4Convert(string sourceString)
        {
            if (!IsIPV4(sourceString))
            {
                throw new Exception("IP不合法!");
            }
            string[] listIP = sourceString.Split('.');
            StringBuilder intIP = new StringBuilder();
            for (int i = 0; i < listIP.Length; i++)
            {
                string ipPart = listIP[i].ToString();
               
                if (ipPart.Length == 3)
                {
                    intIP.Append(ipPart);
                }
                if (ipPart.Length == 2)
                {
                    intIP.Append("0" + ipPart);
                }
                if (ipPart.Length == 1)
                {
                    intIP.Append("00" + ipPart);
                }
            }
            return Convert.ToDouble(intIP.ToString());
        }
        /// <summary>
        /// 正则表达式判断字符串格式
        /// </summary>
        /// <param name="sourceString">要处理的字符串</param>
        /// <param name="strVerdict">正则表达式</param>
        /// <returns>返回结果</returns>
        public static bool FormatVerdict(string sourceString, string strVerdict)
        {
            Regex rx = new Regex(strVerdict);
            return (rx.IsMatch(sourceString));
        }
        /// <summary>
        /// 从字符串中的尾部删除指定的字符串
        /// </summary>
        /// <param name="sourceString">需要处理的字符串</param>
        /// <param name="removedString">删除匹配字符串</param>
        /// <returns>处理后字符串</returns>
        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;
            }
        }
        /// <summary>
        /// 获取拆分符右边的字符串
        /// </summary>
        /// <param name="sourceString"></param>
        /// <param name="splitChar"></param>
        /// <returns></returns>
        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;
        }
        /// <summary>
        /// 获取拆分符左边的字符串
        /// </summary>
        /// <param name="sourceString"></param>
        /// <param name="splitChar"></param>
        /// <returns></returns>
        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;
        }
        /// <summary>
        /// 去掉最后一个逗号
        /// </summary>
        /// <param name="origin"></param>
        /// <returns></returns>
        public static string DelLastComma(string origin)
        {
            if (origin.IndexOf(",") == -1)
            {
                return origin;
            }
            return origin.Substring(0, origin.LastIndexOf(","));
        }
        /// <summary>
        /// 删除不可见字符
        /// </summary>
        /// <param name="sourceString"></param>
        /// <returns></returns>
        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();
        }
        /// <summary>
        /// 获取数组元素的合并字符串
        /// </summary>
        /// <param name="stringArray"></param>
        /// <returns></returns>
        public static string GetArrayString(string[] stringArray)
        {
            string totalString = null;
            for (int i = 0; i < stringArray.Length; i++)
            {
                totalString = totalString + stringArray[i];
            }
            return totalString;
        }
        /// <summary>
        /// 获取某一字符串在字符串数组中出现的次数
        /// </summary>
        /// <param name="stringArray">输入要查询的字符串</param>
        /// <param name="findString">输入操作字符串</param>
        /// <returns>返回出现次数</returns>
        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;
        }
        /// <summary>
        /// 获取某一字符串在字符串中出现的次数
        /// </summary>
        /// <param name="sourceString">输入操作字符串</param>
        /// <param name="findString">要查询的字符</param>
        /// <returns>返回出现次数</returns>
        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;
        }
        /// <summary>
        /// 截取从startString开始到原字符串结尾的所有字符
        /// </summary>
        /// <param name="sourceString">需要操作的字符串</param>
        /// <param name="startString">开始字符</param>
        /// <returns>返回结果</returns>
        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 "";
            }
        }
        /// <summary>
        /// 删除从beginRemovedString到 endRemovedString的字符串
        /// </summary>
        /// <param name="sourceString">需要操作的字符串</param>
        /// <param name="beginRemovedString">开始字符</param>
        /// <param name="endRemovedString">结束字符</param>
        /// <returns>返回结果</returns>
        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; ;
            }
        }
        /// <summary>
        /// 按字节数取出字符串的长度
        /// </summary>
        /// <param name="strTmp">要计算的字符串</param>
        /// <returns>字符串的字节数</returns>
        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;
        }
        /// <summary>
        /// 按字节数要在字符串的位置
        /// </summary>
        /// <param name="intIns">字符串的位置</param>
        /// <param name="strTmp">要计算的字符串</param>
        /// <returns>字节的位置</returns>
        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;
        }
        /// <summary>
        /// 截取输入最大的字符串
        /// </summary>
        /// <param name="text">输入字符串</param>
        /// <param name="maxLength">最大长度</param>
        /// <returns>返回操作结果</returns>
        public static string InputText(string sourceString, int maxLength)
        {
            sourceString = sourceString.Trim();
            if (string.IsNullOrEmpty(sourceString))
                return string.Empty;
            if (sourceString.Length > maxLength)
                sourceString = sourceString.Substring(0, maxLength);
            sourceString = Regex.Replace(sourceString, "[\\s]{2,}", " "); //two or more spaces
            sourceString = Regex.Replace(sourceString, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br>
            sourceString = Regex.Replace(sourceString, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); //
            sourceString = Regex.Replace(sourceString, "<(.|\\n)*?>", string.Empty); //any other tags
            sourceString = sourceString.Replace("'", "''");
            return sourceString;
        }
        /// <summary>
        /// 转义操作
        /// </summary>
        /// <param name="sourceString">输入操作字符串</param>
        /// <returns>返回操作结果</returns>
        public static string Encode(string sourceString)
        {
            sourceString = sourceString.Replace("'", "'");
            sourceString = sourceString.Replace("\"", """);
            sourceString = sourceString.Replace("<", "<");
            sourceString = sourceString.Replace(">", ">");
            return sourceString;
        }
        /// <summary>
        /// 反义操作
        /// </summary>
        /// <param name="sourceString">输入操作字符串</param>
        /// <returns>返回操作结果</returns>
        public static string Decode(string sourceString)
        {
            sourceString = sourceString.Replace(">", ">");
            sourceString = sourceString.Replace("<", "<");
            sourceString = sourceString.Replace(" ", " ");
            sourceString = sourceString.Replace(""", "\"");
            return sourceString;
        }
        /// <summary>
        /// 字符传的转换 用在查询 登陆时 防止恶意的盗取密码
        /// </summary>
        /// <param name="sourceString">输入操作字符串</param>
        /// <returns>返回操作结果</returns>
        public static string TBCode(string sourceString)
        {
            sourceString = sourceString.Replace("!", "");
            sourceString = sourceString.Replace("@", "");
            sourceString = sourceString.Replace("#", "");
            sourceString = sourceString.Replace("$", "");
            sourceString = sourceString.Replace("%", "");
            sourceString = sourceString.Replace("^", "");
            sourceString = sourceString.Replace("&", "");
            sourceString = sourceString.Replace("*", "");
            sourceString = sourceString.Replace("(", "");
            sourceString = sourceString.Replace(")", "");
            sourceString = sourceString.Replace("_", "");
            sourceString = sourceString.Replace("+", "");
            sourceString = sourceString.Replace("|", "");
            sourceString = sourceString.Replace("?", "");
            sourceString = sourceString.Replace("/", "");
            sourceString = sourceString.Replace(".", "");
            sourceString = sourceString.Replace(">", "");
            sourceString = sourceString.Replace("<", "");
            sourceString = sourceString.Replace("{", "");
            sourceString = sourceString.Replace("}", "");
            sourceString = sourceString.Replace("[", "");
            sourceString = sourceString.Replace("]", "");
            sourceString = sourceString.Replace("-", "");
            sourceString = sourceString.Replace("=", "");
            sourceString = sourceString.Replace(",", "");
            return sourceString;
        }
    }
}

 

C#语言和类库

就象微软一贯的风格,C#语言非常的方便。当你想快速的构造一个应用程序的时候,C#语言的效率非常的高。很显然,微软的类库和默认的处理机制在发挥着作用。作为一个程序员,你可以不用关系这些;因为在大多数的情...
  • zade
  • zade
  • 2006年01月04日 15:46
  • 2348

C#通用类库及通用数据库类库

  • 2015年05月07日 14:16
  • 18.99MB
  • 下载

c# DotNet通用类库基类库全集

  • 2014年05月21日 16:19
  • 2.38MB
  • 下载

C#常用类库(100多个)

  • 2014年12月09日 09:06
  • 5.79MB
  • 下载

[HTTP]_[C/C++]_[解析URL的转义字符百分比字符串]

场景: 1.有时候获取一个超链接时,或者一个图片src时,里面的地址带有%XX,这样如果当成文件路径处理会不识别。所以要把转义字符解码。 2.它其实就是ASCII码的十六进制表示。 以下是sta...
  • infoworld
  • infoworld
  • 2013年11月11日 13:58
  • 3894

转义字符

转义字符 \ ·一种特殊的字符常量; ·以反斜线"\"开头,后跟一个或几个字符。 ·具有特定的含义,不同于字符原有的意义,故称“转义”字符。 ·主要用来表示那些用一般字符不便于表示的控制代码。...
  • u013084746
  • u013084746
  • 2016年09月29日 15:31
  • 433

通用C# SqlHelper类库

using System; using System.Collections.Generic; using System.Text; using System.Data; us...
  • SnailKingF
  • SnailKingF
  • 2017年11月04日 10:23
  • 102

分享通用基类库-C#通用缓存类

/************************************************************************************* * 代码:吴蒋 * 时...
  • wujiang1984
  • wujiang1984
  • 2012年03月30日 13:37
  • 3128

c++中的转义字符

http://blog.csdn.net/han1558249222/article/details/50412072char s[]="\\123456\123456\t";printf("%d\n...
  • caimouse
  • caimouse
  • 2016年09月05日 08:57
  • 919

C/C++类库库函数的帮助文档

  • 2011年05月17日 19:50
  • 122KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:分享通用基类库-C#通用字符串处理类
举报原因:
原因补充:

(最多只允许输入30个字)