JavaScript 的 replace 函數(shù)
來(lái)源:程序員人生 發(fā)布時(shí)間:2013-12-05 12:00:55 閱讀次數(shù):3126次
public string EnCode(string content)
{
string str1=content.Replace("<","<");
string str2=str1.Replace(">",">");
string str3=str2.Replace("'","'");
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("'","'");
string str6=str5.Replace(">",">");
string str7=str6.Replace("<","<");
return str7;
這是用來(lái)做sql 轉(zhuǎn)義字符轉(zhuǎn)換的。剛開(kāi)始發(fā)現(xiàn)只轉(zhuǎn)換了第一個(gè)而已。 當(dāng)時(shí)就郁悶了。 用最笨的方法我做兩個(gè)循環(huán)來(lái)替換其他的。后面才發(fā)現(xiàn)只要改為: string str1=content.Replace/&/g,"&"); 就可以替換全部了。
public string EnCode(string content)
{
string str1=content.replace(/</g,"<");
string str2=str1.replace(/>/g,">");
}