For example http://www.vbforums.com/newthread.php?do=newthread&f=25
wanted to replace
http://www.vbforums.com to http://www.youtube.com
which would be better? Delete did not seem to work for me
Printable View
For example http://www.vbforums.com/newthread.php?do=newthread&f=25
wanted to replace
http://www.vbforums.com to http://www.youtube.com
which would be better? Delete did not seem to work for me
vb Code:
Dim strURL As String = "http://www.vbforums.com/newthread.php?do=newthread&f=25" strURL = strURL.Replace("http://www.vbforums.com", "http://www.youtube.com")
How odd. Well i dont understand why i used that before
for example
returns same string? If i make dim as as string = _sCode:Private _s As String = "http://www.vbforums.com/newthread.php?do=newthread&f=25"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
_s.Replace("http://www.vbforums.com", "http://www.youtube.com")
MsgBox(_s)
End Sub
it works
Replace is a FUNCTION ... ir RETURNS the string with the replacements in it. Look at what Sick posted:
Now compare that with your attempt:Code:strURL = strURL.Replace("http://www.vbforums.com", "http://www.youtube.com")
In fact, if you read the documentation for string.replace, you would see that in the description it says:Code:_s.Replace("http://www.vbforums.com", "http://www.youtube.com")
-tgQuote:
Originally Posted by String.Replace_in_MSDN
Yeh i just noticed that. thanks for the break down :)
If you are doing a lot of string replacements consider using stringbuilder
Code:Dim _sb As New System.Text.StringBuilder
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
_sb.Append("http://www.vbforums.com/newthread.php?do=newthread&f=25")
_sb.Replace("http://www.vbforums.com", "http://www.youtube.com")
Debug.WriteLine(_sb.ToString)
_sb.Length = 0 'clear the string builder
End Sub
Could string builder detect and strip everything after a certain char?
example
http://www.youtube.com/watch?v=_qQpLi6tuSM
http://www.youtube.com/watch
or
<urlhere>/audio/27XQR7Tk19XD?stream_token=aMXm0
<urlhere>/audio/27XQR7Tk19XD
Not directly. You could do this but it creates strings which is what we were try to avoid.
Code:Dim sb As New System.Text.StringBuilder
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
sb.Append("http://www.vbforums.com/newthread.php?do=newthread&f=25")
sb.Replace("http://www.vbforums.com", "http://www.youtube.com")
Debug.WriteLine(sb.ToString)
Dim idx As Integer = sb.ToString.IndexOf("?")
If idx <> -1 Then
sb.Remove(idx, sb.Length - idx)
End If
Debug.WriteLine(sb.ToString)
sb.Length = 0 'clear the string builder
End Sub