【问题标题】:String Replace "&" in VB.netVB.net中的字符串替换“&”
【发布时间】:2011-04-15 02:07:11
【问题描述】:

如何在 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

然后它去行替换,它没有。为什么??? 好吧,我想通了……

“&”与“&”不一样,所以我从手表上复制并粘贴它并修复它。他们怎么不一样?不同的编码?

谢谢!

【问题讨论】:

    标签: vb.net string replace


    【解决方案1】:

    你可以这样做:

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

    但如果是因为一个url,你应该看看this post

    您可以改为查看 System.Web.HttpServerUtility.HtmlEncode。

    【讨论】:

    • If Not strOne Is Nothing Then stryOne = strIndustryOne.Replace("&", "&") Else strOne = String.Empty End If
    【解决方案2】:
    Dim str2 As String = str.Replace("&", "&")
    

    【讨论】:

    • 对不起,我刚刚和一个一直在谈论VB.NET多么愚蠢和难以使用的人争论完,我会拿出来。
    • 我在上面看到了你的代码,它到底是怎么回事?如果你的意思是替换 &加上 & 就是:Dim str2 As String = str.Replace("&", "&")
    猜你喜欢
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 2020-11-17
    • 2012-10-01
    • 2017-01-04
    • 1970-01-01
    相关资源
    最近更新 更多