CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jan 2012
    Posts
    35

    Replace Carrage control in text in VB5

    VB5 VB5

    Using VB5, how can I replace a carrage control chr(13) with "</br>" in the middle of a text string?

    VB5 doesn't seem to have a Replace instruction.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Replace Carrage control in text in VB5

    Well, why use VB5 when Visual Studio 2010 Express is FREE? Download VB.Net or C# or C++ and learn to code!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Replace Carrage control in text in VB5

    If memory serves VB5 has a replace function but only on TextBox controls.

    You could always use a combination of Instr() Left() Right() or Mid() to locate and replace the CR.

    As for VS 2010. Would take about 10 times longer to install it than it would to write the code in VB5.
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Replace Carrage control in text in VB5

    Actually I had a quick peek and you are in luck. Turns out I still have a replace function I wrote about 12 years ago in VB5

    Code:
    Public Function ReplaceMe(Source As String, Target As String, Replacement As String) As String
    
        Dim Startpos As Integer
        Dim Ltemp As String
        Dim Rtemp As String
        
        Startpos = InStr(Source, Target)
             
        Do While Startpos > 0
            Ltemp = Left$(Source, Startpos - 1)
            If Len(Source) - (Startpos + Len(Target)) > 0 Then
                Rtemp = Right$(Source, Len(Source) - (Startpos + Len(Target)))
            Else
                Rtemp = ""
            End If
            Source = Ltemp + Replacement + Rtemp
            Startpos = InStr(Source, Target)
       Loop
       ReplaceMe = Source
    End Function
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Replace Carrage control in text in VB5

    The RegEx Library? I wonder how long he searched for a REPLACE function (online as well as in front of the screen coding?)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Replace Carrage control in text in VB5

    Personally I could write a replace function in VB5 in about 10-15 minutes Would take at least twice that long to download VS2010, about 10 times that long to install it and who knows how long to figure out the regex pattern which of course would not be needed for the task at hand.
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Replace Carrage control in text in VB5

    I actually agree. I have numerous VB 6 projects still out there, which I will not rewrite in VS 2010 or almost ( 2011 )!

    I think Dexter is in a position where he / she must use what he / she has. At first, I was also puzzled with the whole idea of having VB 5 with the latest Office versions. Still, there is most likely a good reason for this - who are we to judge / question?

    But, one day a move towards .NET would be a good idea DexterRose

  8. #8
    Join Date
    Jan 2012
    Posts
    35

    Re: Replace Carrage control in text in VB5

    Thanks for the code DataMizer. I almost had it, but your function is much better than the code I was using.

    I still have a problem with the string I am searching. The string contains both VbCrVbCr(enter key pressed twice) and VbCr(enter key pressed once). I can't get the function to work using VbCrVbCr. The best I can do is use chr(13) as the Target in your function and then it returns "sometext</p></p>sometext</p>sometext" whereas
    what I want to return is "sometext<p>sometext</p>sometext".

    It is not recognizing the VbCrVbCr correctly. I have looked at the Hex code and it is "0D0A0D0A"
    How can I correct this?

    The second question I have is: I tried to run your function twice, the first time looking for chr(13) and the second time looking for "</p></p>". The second time the function ran, it truncated the first letter after the "</p></p>". I can't figure that out.


    Also thanks to you dglienna. I agree with you about VB5. Thanks to you suggesting on one of my previous posts, I have decide to convert over to VS2010 later this year(I hope), but for the present time I need to use VB5 to solve the immediate problem. Time is critical here.

    Thanks for all of you helping with this!!!!!!!!!!!!

  9. #9
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Replace Carrage control in text in VB5

    VbCrVbCr is not valid Vb would think you are using a variable that you have not defined and you would actually be passing an empty string so nothing would happen. This assumes that you are not using Option Explicit which btw should always be used. If you were using Option Explicit then the IDE would have given you are error message and flagged the VbCrVbCr as an undefined variable.

    To serach for double CRs it would be VbCr & VbCr or it could also be Chr(13) & Chr(13)

    Not sure why it might drop a character, would have to step through the code to see.
    That function is very old and was used only in a few cases mostly to remove embedded quotes from data in preparing SQL statements.

    If it is dropping a character it would be from this line
    Rtemp = Right$(Source, Len(Source) - (Startpos + Len(Target)))
    but if it were going to happen it should happen all the time.

    Upgrading to .Net is a good idea but for existing projects you really should do a total re-write if you want to use them in .Net

    If you have access to VB6 I would strongly urge you to use that for existing VB5 projects as it will work very well on VB5 projects and gives you more features including a build in replace function.


    sometext<p>sometext</p>sometext
    In any case you will not get the result above using a replace function as there is no way to determine which place to put what. If your intention is to create a line break you could use <br> or <br/> which should work fine. Using <p> it should be <p>some text</p><p>some text </p>
    Last edited by DataMiser; February 16th, 2012 at 01:57 PM.
    Always use [code][/code] tags when posting code.

  10. #10
    Join Date
    Jan 2012
    Posts
    35

    Re: Replace Carrage control in text in VB5

    I had tried the vbcr & vbcr and chr(13) & chr(13) but it doesn't work.

    It's not passing the first line in the function:
    Startpos = InStr(SearchString, WhatToReplace)

    Startpos = 0 using both vbcr & vbcr and chr(13) & chr(13).

    If WhatToReplace is defined as String in the function, could this be the problem? I've tried as Variant and ByVal but that didn't work either.

    Thanks again for your help!!

  11. #11
    Join Date
    Jan 2012
    Posts
    35

    Re: Replace Carrage control in text in VB5

    This is the line of code I am using to call your function:

    LineFirstPass = ReplaceMe(txtDlrStayInTouchLine1, vbCr & vbCr, "</p>")

  12. #12
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Replace Carrage control in text in VB5

    If the text actually has back to back carriage returns then either VbCr & VbCr or CHR(13) & Chr(13)

    If however it has a CRLF as it would if it were a line from atypical file then it will not work because the back to back CR will not be found. In this case you would need to use VBCrLf & VbCrLf or you could use Chr(13) & Chr(10) & Chr(13) & Chr(10)

    If however you do suceed in replacing all these with </p> you will still have an issue as </p> expects there to be a matching <p>

    Why are you not using <br> ?
    Last edited by DataMiser; February 16th, 2012 at 03:23 PM.
    Always use [code][/code] tags when posting code.

  13. #13
    Join Date
    Jan 2012
    Posts
    35

    Re: Replace Carrage control in text in VB5

    Finally. Success!!

    I needed to search for "vbcr & vblf & vbcr & vblf". After I looked at the Hex code this should have been obvious - must be too tired.

    The truncating problem in the function I solved by adding + 1 in the revised function below:

    Private Function ReplaceMe(SearchString As String, WhatToReplace As String, ReplaceWith As String) As String
    Dim Startpos As Integer
    Dim LeftPart As String
    Dim RightPart As String
    Dim LenWhatToReplace As Integer, LenSearchString As Integer

    Startpos = InStr(SearchString, WhatToReplace)

    Do While Startpos > 0
    LeftPart = Left$(SearchString, Startpos - 1)
    If Len(SearchString) - (Startpos + Len(WhatToReplace)) > 0 Then
    RightPart = Right$(SearchString, ((Len(SearchString) + 1) - (Startpos + Len(WhatToReplace))))
    Else
    RightPart = ""
    End If
    SearchString = LeftPart + ReplaceWith + RightPart
    Startpos = InStr(SearchString, WhatToReplace)
    Loop
    ReplaceMe = SearchString
    End Function


    These are my two function calling instructions:
    LineFirstPass = ReplaceMe(txtDlrStayInTouchLine1, vbCr & vbLf & vbCr & vbLf, "<p>")
    LineNextPass = ReplaceMe(LineFirstPass, vbCr & vbLf, "</br>")

    Many thanks for your help - been a good learning experience!!!!!!!

  14. #14
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Replace Carrage control in text in VB5

    While your <p> may work in most browsers this tag is supposed to have a closing tag</p> and may not work in some browsers.

    </br> I'm not sure about. Maybe it will work in some browsers but it is incorrect. <br> is an empty tag that requires no end tag where </br> denotes and end tag for <br> which does not exist. It is supposed to be <br> or in XHTML or XML it would be <br /> since elements without a closing tag are not allowed.


    http://www.w3schools.com/html/html_paragraphs.asp
    Always use [code][/code] tags when posting code.

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

Featured