javascript 的 replace 函数

ContractedBlock.gif ExpandedBlockStart.gif Code
 1None.gif
 2None.gif
 3None.gifpublic string EnCode(string content)
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif   string str1=content.Replace("<","&lt;");
 6InBlock.gif   string str2=str1.Replace(">","&gt;");
 7InBlock.gif   string str3=str2.Replace("'","&apos;");
 8InBlock.gif   string str4=str3.Replace(" ","&nbsp;");
 9InBlock.gif   string str5=str4.Replace("\r\n","<br>");
10InBlock.gif   string str6=str5.Replace("\"","&quot;");
11InBlock.gif   string str7=str6.Replace("&","&amp;");
12InBlock.gif   return str7;           
13ExpandedBlockEnd.gif}

14None.gif
15None.gifpublic string UnCode(string content)
16ExpandedBlockStart.gifContractedBlock.gifdot.gif{
17InBlock.gif   string str1=content.Replace("&amp;","&");
18InBlock.gif   string str2=str1.Replace("&quot;","\"");
19InBlock.gif   string str3=str2.Replace("<br>","\r\n");
20InBlock.gif   string str4=str3.Replace("&nbsp;"," ");
21InBlock.gif   string str5=str4.Replace("'","&apos;");
22InBlock.gif   string str6=str5.Replace("&gt;",">");
23InBlock.gif   string str7=str6.Replace("&lt;","<");   
24InBlock.gif   return str7;
25ExpandedBlockEnd.gif}

 

这是用来做sql 转义字符转换的。刚开始发现只转换了第一个而已。 当时就郁闷了。 用最笨的方法我做两个循环来替换其他的。

后面才发现只要改为: string str1=content.Replace/&amp;/g,"&"); 就可以替换全部了。

 

public   string  EnCode( string  content)
{
   
string  str1 = content.replace( /</ g, " &lt; " );
   
string  str2 = str1.replace( />/ g, " &gt; " );
}

 

转载于:https://www.cnblogs.com/novus/archive/2009/07/17/1525310.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值