简体   繁体   中英

vb.net how to get rid of unwanted vblf char

Hi everyone can someone explain to me why he vblf character keeps getting in my string please? i use this code so i don't have to check if the http:// already exists in the string with an 'if'

url1 = "http://" + URL.replace("http://","").Split("/")(0) & strin2

the problem is the result url is like this : "http://" & vblf & 'the rest of the url can anyone explain to me why the vblf keeps getting in my string?

I would really use the right tool for the job which seems to be the Uri class :

Dim url As String = "http://google.com/blah?foo=1"
Dim uri As Uri
If Uri.TryCreate(url, UriKind.Absolute, uri) Then
    Dim schemeAndHost As String = uri.Scheme + uri.SchemeDelimiter + uri.Host
End If

Result: http://google.com

If you don't know if the url contains the protocol you could use the UriBuilder class:

Dim url As String = "google.com/blah?foo=1"
Dim schemeAndHost As String
Dim uri As Uri = Nothing
If uri.TryCreate(url, UriKind.Absolute, uri) Then
    schemeAndHost = uri.Scheme + uri.SchemeDelimiter + uri.Host
ElseIf url.Contains("/") Then
    uri = New UriBuilder("http", url.Remove(url.IndexOf("/"))).Uri
    schemeAndHost = uri.Scheme + uri.SchemeDelimiter + uri.Host
End If

(same result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM