Results 1 to 6 of 6

Thread: Text Compare and replace

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Location
    London, UK
    Posts
    137

    Red face

    Hi all,

    I am having trouble trying to find a Char in a fixed position and then replacing it if it exists.

    I was using something like this:
    ElseIf Right(DSCode, 1) = "." Then
    DSCstr = Replace("DSCode", ".", "_", , 1, vbTextCompare)

    But It does not work (no laughing pls, I am still in the novice learning stage)

    Can any one point me in the right direction pls.

    Any help / advice would be great.

    Thanks in advance,
    Rocks.
    join me in the platinum

  2. #2
    Hyperactive Member WP's Avatar
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    278

    Talking

    Try this:
    Code:
    'Text1.Text = "This is my line."
    Private Sub Command1_Click()
       Call Char_Change(1, ".", "!")'just change this line
    End Sub
    
    Private Sub Char_Change(Number, FromWhat, ToWhat)
       If Mid(Text1.Text, Len(Text1) - Number + 1, 1) = FromWhat Then
          Text1.SelStart = Len(Text1) - Number
          Text1.SelLength = 1
          Text1.SelText = ToWhat
       End If
    End Sub
    WP

    Visual Basic 6.0 EE SP5 / .Net
    Windows XP

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Location
    London, UK
    Posts
    137

    Red face Replace & Compare

    Thanks For that,

    I also need to do a similar process with the second char in the string changing the ':' to '@'....


    But these are conditional on if the string contains either a ':' or a '.'.........

    Is there another way I could do that???



    Thanks in advance.....

    Rocks
    join me in the platinum

  4. #4
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Dim DSCode As String, sTemp As String
        Dim sLen As Integer
        
        'for this example this is your string
            DSCode = "www.is this it or am I barking up the wrong tree."
        'assuming if you are looking right 1 for a decimal
        'it will always be the last character of the string
        '
        'in that case the length of your string will always
        'be len(yourstring - 1)
        '
        sLen = Len(DSCode)
        sLen = sLen - 1
        sTemp = Left(DSCode, (sLen))
        
        If Right(DSCode, 1) = "." Then
           sTemp = Left(DSCode, sLen)
           DSCode = sTemp & "_"
        End If
          MsgBox DSCode
    End Sub
    In vb6 there is a replace statement
    if you know for sure there is only one "." in your
    string, then you would do it like this


    DSCode = Replace(DSCode, ".", "_")
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  5. #5
    Lively Member
    Join Date
    Aug 2000
    Location
    Australia
    Posts
    82
    The way I like to do this sort of thing is to use mid in both its function and statement form. See below.

    Dim ThisText as string
    Dim i as integer

    'Example. Replace the "_" and the "@" chars with spaces.

    ThisText = "This_is a line@of text"

    For i = 1 to Len(ThisText)
    Select case Mid(ThisText, i, 1)
    case "_", "@"
    mid(ThisText,i, 1) = Chr(32)
    end select
    next i

    Cheers
    Adrian

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Location
    London, UK
    Posts
    137

    Cool Thanks all

    Thank you all,

    I have managed to get it sorted with your help.


    Regards,

    Rocks
    join me in the platinum

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