我有一个使用 .Net framework 4.0 的 ASP.Net 项目。如果我的项目发布到我的 Windows 7 工作站,则以下行可以正常工作:
strTemplate = strTemplate.Replace("<span style=\"background-color: yellow;\">", "");
但是,如果我将项目发布到 Windows 2008 R2 服务器,则不会发生上述替换。没有错误;服务器只是找不到模式并且不会替换它。谁能告诉我为什么,或者如何解决这个问题?我试过在我的模式字符串前面放一个“@”,但是字符串想要在“background-color”之前的双引号处终止,无论反斜杠是否存在。
最佳答案
试试这个:
strTemplate = strTemplate.Replace(@"<span style=""background-color: yellow;"">", "");
使用@
符号时必须加上两个双引号。
编辑: 假设您有权访问 Windows 2008 R2 服务器上正在运行的应用程序,请尝试在 Windows 7 工作站上运行此代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Globalization;
namespace CurrentCulture
{
public class Info : MarshalByRefObject
{
public void ShowCurrentCulture()
{
Console.WriteLine("Culture of {0} in application domain {1}: {2}",
Thread.CurrentThread.Name,
AppDomain.CurrentDomain.FriendlyName,
CultureInfo.CurrentCulture.Name);
}
}
class Program
{
static void Main(string[] args)
{
Info inf = new Info();
// Set the current culture to Dutch (Netherlands).
Thread.CurrentThread.Name = "MainThread";
Thread.CurrentThread.CurrentCulture = CultureInfo.CurrentCulture;
inf.ShowCurrentCulture();
Console.Read();
}
}
}
如果 Windows 7 工作站上的当前区域性与 Windows 2008 R2 服务器上的当前区域性不同,您将需要调整 Windows 2008 R2 线程的当前区域性以符合您的预期。
您可以像这样将线程的文化设置为新文化:
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("nl-NL");
我的代码是从 MSDN 页面修改的:http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentculture(v=vs.110).aspx
关于c# - ASP.Net 的 String.Replace 在 Windows 7 工作站上运行时与在 Windows 2008 R2 服务器上运行时的行为不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16322829/