Results 1 to 8 of 8

Thread: [RESOLVED] String manipulation _ Replace vs delete

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Resolved [RESOLVED] String manipulation _ Replace vs delete

    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

  2. #2
    Lively Member
    Join Date
    May 2006
    Posts
    70

    Re: String manipulation _ Replace vs delete

    vb Code:
    1. Dim strURL As String = "http://www.vbforums.com/newthread.php?do=newthread&f=25"
    2. strURL = strURL.Replace("http://www.vbforums.com", "http://www.youtube.com")

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    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

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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:
    Quote 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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Re: String manipulation _ Replace vs delete

    Yeh i just noticed that. thanks for the break down

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    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

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width