ASP.NET----网站字符串操作类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Core.Common.Web
{
    /// <summary>
    /// 网站字符串操作类
    /// </summary>
    public static class String
    {
        #region 静态方法

        /// <summary>
        /// 左截取
        /// </summary>
        /// <param name="inputString">字符串</param>
        /// <param name="length">长度</param>
        /// <returns>截取后的字符串</returns>
        public static string Left(string inputString, int length)
        {
            if (inputString.Length < length)
                return inputString;
            else
                return inputString.Substring(0, length);
        }

        /// <summary>
        /// 左截取
        /// </summary>
        /// <param name="inputString">字符串</param>
        /// <param name="length">长度</param>
        /// <param name="addString">截取后添加的字符串</param>
        /// <returns>截取后的字符串</returns>
        public static string Left(string inputString, int length, string addString)
        {
            if (inputString.Length < length)
                return inputString;
            else
                return inputString.Substring(0, length) + addString;
        }

        /// <summary>
        /// 右截取
        /// </summary>
        /// <param name="inputString">字符串</param>
        /// <param name="length">长度</param>
        /// <returns>截取后的字符串</returns>
        public static string Right(string inputString, int length)
        {
            if (inputString.Length < length)
                return inputString;
            else
                return inputString.Substring(inputString.Length - length, length);
        }

        /// <summary>
        /// 不区分大小写的替换
        /// </summary>
        /// <param name="original">原字符串</param>
        /// <param name="pattern">需替换字符</param>
        /// <param name="replacement">被替换内容</param>
        /// <returns>替换后的字符串</returns>
        public static string ReplaceEx(string original, string pattern, string replacement)
        {
            int count = 0;
            int position0 = 0;
            int position1 = 0;
            string upperString = original.ToUpper();
            string upperPattern = pattern.ToUpper();
            int inc = (original.Length / pattern.Length) * (replacement.Length - pattern.Length);
            char[] chars = new char[original.Length + Math.Max(0, inc)];
            while ((position1 = upperString.IndexOf(upperPattern, position0)) != -1)
            {
                for (int i = position0; i < position1; ++i) chars[count++] = original[i];
                for (int i = 0; i < replacement.Length; ++i) chars[count++] = replacement[i];
                position0 = position1 + pattern.Length;
            }
            if (position0 == 0) return original;
            for (int i = position0; i < original.Length; ++i) chars[count++] = original[i];
            return new string(chars, 0, count);
        }

        /// <summary>
        /// 替换html中的特殊字符
        /// </summary>
        /// <param name="theString">需要进行替换的文本</param>
        /// <returns>替换完的文本</returns>
        public static string HtmlEncode(string theString)
        {
            theString = theString.Replace(">", ">");
            theString = theString.Replace("<", "<");
            theString = theString.Replace("  ", "  ");
            theString = theString.Replace("\"", """);
            theString = theString.Replace("'", "'");
            theString = theString.Replace("\r\n", "<br/> ");
            return theString;
        }

        /// <summary>
        /// 恢复html中的特殊字符
        /// </summary>
        /// <param name="theString">需要恢复的文本。</param>
        /// <returns>恢复好的文本。</returns>
        public static string HtmlDecode(string theString)
        {
            theString = theString.Replace(">", ">");
            theString = theString.Replace("<", "<");
            theString = theString.Replace("  ", "  ");
            theString = theString.Replace(""", "\"");
            theString = theString.Replace("'", "'");
            theString = theString.Replace("<br/> ", "\r\n");
            return theString;
        }

        /// <summary>
        /// 将html转成js代码,不完全和原始数据一致
        /// </summary>
        /// <param name="source">html文本</param>
        /// <returns>js代码</returns>
        public static string HtmlToJs(string source)
        {
            return string.Format("document.write(\"{0}\");",
                string.Join("\");\r\ndocument.write(\"", source.Replace("\\", "\\\\")
                                        .Replace("/", "\\/")
                                        .Replace("'", "\\'")
                                        .Replace("\"", "\\\"")
                                        .Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
                            ));
        }

        /// <summary>
        /// 将html转成可输出的js字符串,不完全和原始数据一致
        /// </summary>
        /// <param name="source">html文本</param>
        /// <returns>js文本</returns>
        public static string HtmlToJsString(string source)
        {
            return string.Format("{0}",
                string.Join(" ", source.Replace("\\", "\\\\")
                                        .Replace("/", "\\/")
                                        .Replace("'", "\\'")
                                        .Replace("\"", "\\\"")
                                        .Replace("\t", "")
                                        .Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
                            ));
        }

        /// <summary>
        /// 过滤所有特殊特号
        /// </summary>
        /// <param name="theString">需过滤的字符串</param>
        /// <returns>过滤后的字符串</returns>
        public static string FilterSymbol(string theString)
        {
            string[] aryReg = { "'", "\"", "\r", "\n", "<", ">", "%", "?", ",", ".", "=", "-", "_", ";", "|", "[", "]", "&", "/" };
            for (int i = 0; i < aryReg.Length; i++)
            {
                theString = theString.Replace(aryReg[i], string.Empty);
            }
            return theString;
        }

        /// <summary>
        /// 过滤一般特殊特号,如单引、双引和回车和分号等
        /// </summary>
        /// <param name="theString">需过滤的字符串</param>
        /// <returns>过滤后的字符串</returns>
        public static string SafetyString(string theString)
        {
            string[] aryReg = { "'", ";", "\"", "\r", "\n", "<", ">" };
            for (int i = 0; i < aryReg.Length; i++)
            {
                theString = theString.Replace(aryReg[i], string.Empty);
            }
            return theString;
        }

        /// <summary>
        /// 正则表达式取值
        /// </summary>
        /// <param name="htmlCode">HTML代码</param>
        /// <param name="regexString">正则表达式</param>
        /// <param name="groupKey">正则表达式分组关键字</param>
        /// <param name="rightToLeft">是否从右到左</param>
        /// <returns>匹配的值</returns>
        public static string[] GetRegexValue(string htmlCode, string regexString, string groupKey, bool rightToLeft)
        {
            MatchCollection m;
            Regex r;
            if (rightToLeft == true)
            {
                r = new Regex(regexString, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.RightToLeft);
            }
            else
            {
                r = new Regex(regexString, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            }
            m = r.Matches(htmlCode);
            string[] MatchValue = new string[m.Count];
            for (int i = 0; i < m.Count; i++)
            {
                MatchValue[i] = m[i].Groups[groupKey].Value;
            }
            return MatchValue;
        }

        #endregion
    }
}

  

posted @ 2011-08-16 10:33  brainmao  阅读(584)  评论(3编辑  收藏  举报