控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

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

作者:佚名 出处:互联网 2011年08月17日 阅读:

ASP.NET----网站字符串操作类
 
view sourceprint?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 
    } 
}

 

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统