程序员人生 网站导航

JavaScript 的 replace 函数

栏目:jscript时间:2013-12-05 12:00:55
public string EnCode(string content)
{
string str1=content.Replace("<","<");
string str2=str1.Replace(">",">");
string str3=str2.Replace("'","&apos;");
string str4=str3.Replace(" "," ");
string str5=str4.Replace("","<br>");
string str6=str5.Replace(""",""");
string str7=str6.Replace("&","&");
return str7;
}

public string UnCode(string content)
{
string str1=content.Replace("&","&");
string str2=str1.Replace(""",""");
string str3=str2.Replace("<br>","");
string str4=str3.Replace(" "," ");
string str5=str4.Replace("'","&apos;");
string str6=str5.Replace(">",">");
string str7=str6.Replace("<","<");
return str7;

 这是用来做sql 转义字符转换的。刚开始发现只转换了第一个而已。 当时就郁闷了。 用最笨的方法我做两个循环来替换其他的。后面才发现只要改为: string str1=content.Replace/&amp;/g,"&"); 就可以替换全部了。

public string EnCode(string content)
{
string str1=content.replace(/</g,"<");
string str2=str1.replace(/>/g,">");
}
------分隔线----------------------------
------分隔线----------------------------

最新技术推荐