开发者

String Replace "&" in VB.net

开发者 https://www.devze.com 2023-02-25 10:56 出处:网络
How do I replace a \"&\" in a string in vb.net. I have tried: str2 As String = Replace(str, \"&\"c, \"&\")

How do I replace a "&" in a string in vb.net. I have tried:

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

For some reason it is not working here is the code. I am using a parameter in the URL and using request to store the parameter. It is definitly storing correctly, because when I debug i see that strOne is gettign the correct valeu.

        Dim strOne As String = Request("One")

IN DEBUG HERE WATCHING strOne = "ABC D&E"

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

Then it goes to the line to replace, and it doesnt. Why??? Ok I figured it out....

The "&" is not the same as the "&", so I copied and pasted it from the watch and it fixed it. How come they a开发者_如何学运维re different? Different encoding?

Thanks!


You can do:

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

But if it is because of a url, you should take a look at this post

you might look at System.Web.HttpServerUtility.HtmlEncode instead.


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

精彩评论

暂无评论...
验证码 换一张
取 消