JavaScript 的 replace 函數
來源:程序員人生 發布時間:2013-12-05 12:00:55 閱讀次數:3133次
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;
這是用來做sql 轉義字符轉換的。剛開始發現只轉換了第一個而已。 當時就郁悶了。 用最笨的方法我做兩個循環來替換其他的。后面才發現只要改為: string str1=content.Replace/&/g,"&"); 就可以替換全部了。
public string EnCode(string content)
{
string str1=content.replace(/</g,"<");
string str2=str1.replace(/>/g,">");
}