[英]Replace 3 characters in a string with 3 new values
我想用各自的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("®", "®") +"</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.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());
如果要编码这种字符,则应改用html编码器。
在C#中,您可以使用以下代码:
string result = System.Web.HttpUtility.HtmlEncode("Sample string ©");
如果要一个一个替换一个字符,可以使用Sudhakar Tillapudi的代码。 他的答案是正确的,但是您将需要进行很多替换。
DotNetPerls网站上有一个很好的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:
<html><head><title>T</title></head></html>
After HtmlDecode:
<html><head><title>T</title></head></html>
您可以为每个Replace操作链接Replace()
方法。
尝试这个:
StringBuilder text = new StringBuilder(lit.Text.ToString());
msg.Body = text.Replace("value1", "replace1").Replace("value2",
"replace2").Replace("value3", "replace3");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.