用3个新值替换字符串中的3个字符

时间:2014-02-28 14:51:50

标签: c# html regex replace html-entities

我想用各自的html实体替换版权符号的实例。就像现在一样,版权符号只是直接复制到产品名称中,所以现在,当我发送带有完整订单摘要的电子邮件时,版权符号显示为“?”代替。我需要在字符串中查找(c),(R)和TM版权符号,并用适当的HTML实体代码替换每一个。但是,我不知道如何做这样的String.Replace()(搜索3个字符并用3个新值替换它们对我来说似乎很复杂)

以下是设置电子邮件的代码(现在我只检查(R)符号):

protected void sendEmail()
    {
        Item CurrentItem = Sitecore.Context.Item;
        Item HomeItem = ScHelper.FindAncestor(CurrentItem, "gojoOrderReview");
        if (HomeItem != null)
        {
            TextBox rTxt = (TextBox)FindControl("rEmail");
            TextBox fName = (TextBox)FindControl("yName");
            Literal lit = (Literal)FindControl("emailTbl");
            //string html = lit.Text;

                try
                {
                    //.Value = string.Format("<tr><td style="width:80px;">{0}</td><td style="width:50px;">{1}</td><td style="width:20px;>{2}</td><td style="width:20px;>{3}</td><td style="width:60px;>{4}</td></tr>", SkuItem.Fields["Order Number"].Value, SkuItem.Fields["Description"].Value, qtyAmt);
                    System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
                    msg.To.Add(rTxt.Text);
                    msg.Subject = "GOJO product order summary";
                    msg.From = new System.Net.Mail.MailAddress("donotreply@GOJO.com");
                    msg.Body = "Here is a list of the products your friend, " + fName.Text + ", has selected: ";
                    msg.IsBodyHtml = true;

                    msg.Body += "<h1></h1>";
                    msg.Body += "<table cellpadding='4' cellspacing='0' border='1px solid black' style='padding: 4px 0 4px 0; border-collapse: collapse; width:750px; text-align: left; font-family: sans-serif;'><tr><th></th><th>Product</th><th>GOJO SKU</th><th>Size</th><th>Case Pack</th><th>Quantity</th></tr>";
                    msg.Body += lit.Text.ToString().Replace("®", "&reg;") +"</table>";



                    System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient("mail.innismaggiore.com");
                    sc.Send(msg);
                }
                catch (Exception EX)
                {
                    GOJOHelper.WriteLog("GOJO Order Review - sendEmail()", EX.ToString());
                }

        }
    }

以下是创建lit变量文本值的代码:

lit.Text += string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td></tr>",  drow["Thumbnail"], CPNItem["Complete Name"], marketItem.Fields["SKU"].Value, drow["Size"].ToString(), marketItem.Fields["Case Pack"].Value, qtys.ToString());

2 个答案:

答案 0 :(得分:1)

如果要对这种字符进行编码,则应使用html编码器。

在C#中,你可以使用它:

string result = System.Web.HttpUtility.HtmlEncode("Sample string ©");

如果您想逐个替换每个角色,可以使用Sudhakar Tillapudi的代码。他的答案是有效的,但你会做很多替换。

网站DotNetPerls有good example HtmlEncode

using System;
using System.Net;

class Program
{
    static void Main()
    {
        string a = WebUtility.HtmlEncode("<html><head><title>T</title></head></html>");
        string b = WebUtility.HtmlDecode(a);

        Console.WriteLine("After HtmlEncode: " + a);
        Console.WriteLine("After HtmlDecode: " + b);
    }
}

输出:

After HtmlEncode:
&lt;html&gt;&lt;head&gt;&lt;title&gt;T&lt;/title&gt;&lt;/head&gt;&lt;/html&gt;

After HtmlDecode:
<html><head><title>T</title></head></html>

答案 1 :(得分:0)

您可以为每个替换操作链接Replace()方法。

试试这个:

StringBuilder text = new StringBuilder(lit.Text.ToString());
msg.Body = text.Replace("value1", "replace1").Replace("value2", 
                      "replace2").Replace("value3", "replace3");