|
-
Oct 17th, 2010, 01:41 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] String manipulation _ Replace vs delete
-
Oct 17th, 2010, 02:19 PM
#2
Lively Member
Re: String manipulation _ Replace vs delete
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")
-
Oct 17th, 2010, 02:26 PM
#3
Thread Starter
Fanatic Member
Re: String manipulation _ Replace vs delete
How odd. Well i dont understand why i used that before
for example
Code:
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
returns same string? If i make dim as as string = _s
it works
-
Oct 17th, 2010, 02:50 PM
#4
Re: String manipulation _ Replace vs delete
Replace is a FUNCTION ... ir RETURNS the string with the replacements in it. Look at what Sick posted:
Code:
strURL = strURL.Replace("http://www.vbforums.com", "http://www.youtube.com")
Now compare that with your attempt:
Code:
_s.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:
 Originally Posted by String.Replace_in_MSDN
Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
-tg
-
Oct 17th, 2010, 03:00 PM
#5
Thread Starter
Fanatic Member
Re: String manipulation _ Replace vs delete
Yeh i just noticed that. thanks for the break down
-
Oct 17th, 2010, 03:25 PM
#6
Re: String manipulation _ Replace vs delete
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
-
Oct 17th, 2010, 03:56 PM
#7
Thread Starter
Fanatic Member
Re: [RESOLVED] String manipulation _ Replace vs delete
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
-
Oct 17th, 2010, 05:44 PM
#8
Re: [RESOLVED] String manipulation _ Replace vs delete
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|