C# Replace替换问题 和 除去HTML标签

1、看到好多人遇到字符串Replace替换不成功的问题,大家要注意了

        string aa = "亲爱的$name,感谢您选择该网站,您在该网站上的订单$no已经提交成功,请您在2小时内完成支付,以便我们尽快为您发货,如有问题请电:4007-336-337";
        aa.Replace("$name", "ceshi000");
        aa.Replace("$no", "2012090400032");
        Response.Write(aa);

上面的代码看吧,看不出问题,但是替换的时候没成功,为什么呢?

原因是替换的时候要从新给字符串aa再次赋值,如下:

        string aa = "亲爱的$name,感谢您选择该网站,您在该网站上的订单$no已经提交成功,请您在2小时内完成支付,以便我们尽快为您发货,如有问题请电:4007-336-337";
        aa=aa.Replace("$name", "ceshi000");
        aa=aa.Replace("$no", "2012090400032");
        Response.Write(aa);

这样就能替换成功了。

2、大家做开发的时候,碰到一些内容是含有HTML标签的,有的时候是不需要的,怎么去除?如下:

string Smes= System.Text.RegularExpressions.Regex.Replace(message, "<.*?>", "").Replace("&nbsp;", "");

注:message就是含有HTML标签的内容。

3、上面的那个全部去除了

 protected string gHtml(string Htmlstring)
    {
        try
        {
            if (HTMLTagsCheck.Check(Htmlstring))
            {
                Htmlstring = HTMLTagsCheck.Fix(Htmlstring);
            }
        }
        catch
        { }
        Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
        //删除HTML  
       //Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"<div", "<p", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"</div>", "</p>", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);

        Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
        Htmlstring.Replace("\r\n", "");
       // Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
        if(Htmlstring.IndexOf("img.cdaily.1qfa.com/FY")==-1)
        {
            Htmlstring=Htmlstring.Replace("img.cdaily.1qfa.com","img.cdaily.1qfa.com/FY");
        }
        return Htmlstring;
    }


        /// <summary>
        /// 检查HTML是否存在未闭合的标签
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool Check(String str)
        {
            TagsList[] unclosedTags = getUnclosedTags(str);

            if (unclosedTags[0].Size() != 0)
            {
                return false;
            }
            for (int i = 0; i < unclosedTags[1].Size(); i++)
            {
                if (unclosedTags[1].get(i) != null)
                    return false;
            }

            return true;
        }


        /// <summary>
        /// 修复HTML未闭合的标签
        /// </summary>
        /// <param name="str">要进行修复的标签</param>
        /// <returns></returns>
        public static String Fix(String str)
        {
            try
            {
                StringBuilder sfixed = new StringBuilder(); // 存放修复后的字符串
                TagsList[] unclosedTags = getUnclosedTags(str);

                // 生成新字符串
                for (int i = unclosedTags[0].Size() - 1; i > -1; i--)
                {
                    sfixed.Append("<" + unclosedTags[0].get(i) + ">");
                }

                sfixed.Append(str);

                for (int i = unclosedTags[1].Size() - 1; i > -1; i--)
                {
                    String s = null;
                    if ((s = unclosedTags[1].get(i)) != null)
                    {
                        sfixed.Append("</" + s + ">");
                    }
                }

                return sfixed.ToString();
            }
            catch
            {
                return str;
            }
        }


 private static TagsList[] getUnclosedTags(string _str)
        {
            StringBuilder temp = new StringBuilder(); // 存放标签
            TagsList[] unclosedTags = new TagsList[2];
            unclosedTags[0] = new TagsList(); // 前不闭合,如有</div>而前面没有<div>
            unclosedTags[1] = new TagsList(); // 后不闭合,如有<div>而后面没有</div>
            bool flag = false; // 记录双引号"或单引号'
            char currentJump = ' '; // 记录需要跳过''还是""

            char current = ' ', last = ' '; // 当前 & 上一个

            // 开始判断
            char[] str = _str.ToCharArray();
            for (int i = 0; i < _str.Length; )
            {
                current = str[i++]; // 读取一个字符
                if (current == '"' || current == '\'')
                {
                    flag = flag ? false : true; // 若为引号,flag翻转
                    currentJump = current;
                    if (flag)
                    {
                        while (i < _str.Length && str[i++] != currentJump)
                            ; // 跳过引号之间的部分
                        flag = false;
                    }
                }
                else if (current == '<')
                {
                    if (i >= _str.Length)
                        break;
                    // 开始提取标签
                    current = str[i++];
                    if (current == '/')
                    { // 标签的闭合部分,如</div>
                        current = str[i++];

                        // 读取标签
                        while (i < _str.Length && current != '>')
                        {
                            temp.Append(current);
                            current = str[i++];
                        }

                        // 从tags_bottom移除一个闭合的标签
                        if (!unclosedTags[1].remove(temp.ToString()))
                        { // 若移除失败,说明前面没有需要闭合的标签
                            unclosedTags[0].add(temp.ToString()); // 此标签需要前闭合
                        }
                        temp.Remove(0, temp.Length); // 清空temp
                    }
                    else
                    { // 标签的前部分,如<div>
                        last = current;
                        while (i < _str.Length && current != ' '
                                && current != ' ' && current != '>')
                        {
                            temp.Append(current);
                            last = current;
                            current = str[i++];
                        }

                        // 已经读取到标签,跳过其他内容,如<div id=test>跳过id=test
                        while (i < _str.Length && current != '>')
                        {
                            last = current;
                            current = str[i++];
                            if (current == '"' || current == '\'')
                            { // 判断双引号
                                flag = flag ? false : true;
                                currentJump = current;
                                if (flag)
                                { // 若引号不闭合,跳过到下一个引号之间的内容
                                    while (i < _str.Length
                                            && str[i++] != currentJump)
                                        ;
                                    current = str[i++];
                                    flag = false;
                                }
                            }
                        }
                        if (last != '/' && current == '>') // 判断这种类型:<TagName />
                            unclosedTags[1].add(temp.ToString());
                        temp.Remove(0, temp.Length);
                    }
                }
            }
            return unclosedTags;
        }
    }


 public class TagsList
    {
        private String[] data;
        private int size = 0;

        public TagsList(int size)
        {
            data = new String[size];
        }

        public TagsList()
            : this(10)
        {

        }

        public void add(String str)
        {
            ensureCapacity(size + 1);
            data[size++] = str;
        }

        public String get(int index)
        {
            if (index < size)
                return data[index];
            else
                return null;
        }

        //为了提高效率,只将其置为null
        public bool remove(String str)
        {
            for (int index = size - 1; index >= 0; index--)
            {
                if (str.Equals(data[index]))
                {
                    data[index] = null;
                    return true;
                }
            }
            return false;
        }

        public bool remove(int index)
        {
            if (index < data.Length)
            {
                data[index] = null;
                return true;
            }
            return false;
        }

        public int Size()
        {
            return this.size;
        }

        //扩展容量
        public void ensureCapacity(int minSize)
        {
            int oldCapacity = data.Length;
            if (minSize > oldCapacity)
            {
                int newCapacity = (oldCapacity * 3 / 2 + 1) > minSize ?
                        oldCapacity * 3 / 2 + 1 : minSize;
                string[] newarr = new string[newCapacity];
                Array.Copy(data, newarr, oldCapacity);
                data = newarr;
            }
        }

    }

你可能感兴趣的:(html,String,C#)