2011-04-15 51 views
2

如何在vb.net中的字符串中替换“&”。我曾尝试过:字符串替换VB.net中的“&”

str2 As String = Replace(str, "&"c, "&") 
str2 As String = Replace(str, "&", "&") 
str2 As String = Replace(str, chr(38), "&") 

由于某些原因,这里不工作的代码。我正在使用URL中的参数并使用请求来存储参数。它确实存储正确,因为当我调试时,我发现strOne正在获取正确的值。

 Dim strOne As String = Request("One") 

在调试这里看着strOne = “ABC d&E”

 If Not strOne Is Nothing Then 
      strOne = strOne.Replace("&", "&") 
     Else 
      strOne = String.Empty 
     End If 

然后,它前进到所述线来代替,并且它不。为什么??? 好吧我想通了......

“&”是不一样的“&”,所以我复制并粘贴它从手表,它固定它。他们如何与众不同?不同的编码?

谢谢!

回答

4

你可以这样做:

Dim str2 As String = str.Replace("&", "&") 

但如果是因为一个网址,你应该在this post

看一看你可能看System.Web.HttpServerUtility.HtmlEncode代替。

+0

如果不strOne是Nothing然后 stryOne = strIndustryOne.Replace( “&”, “&”) 否则 strOne =的String.Empty 结束如果 – BigOrangeSU 2011-04-15 02:54:35

0
Dim str2 As String = str.Replace("&", "&") 
+0

一个人不好意思,我刚刚结束争论谁不断谈论VB.NET是如何愚蠢和难以使用,我会把它拿出来。 – Ryan 2011-04-15 02:51:50

+0

由于某些原因,不工作.....这里是我使用的代码: – BigOrangeSU 2011-04-15 02:54:09

+0

我看到你的代码上面,究竟是什么不工作?如果你的意思是用&替换&,它是:Dim str2 As String = str.Replace(“&”,“&”)' – Ryan 2011-04-15 02:56:28