Results 1 to 12 of 12

Thread: Removing information from a string? VB6

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Removing information from a string? VB6

    Hello, I have a *.txt file and inside it contains lines with information (each block of information in a new line). This information has a similar pattern:
    2014.05.26 11:45:00,67.14975845410586

    The next lines have the same structure, the difference is the date value and the number's value.

    In the above line I want to remove the comma and all the information in the left. About the number I want the integer part and the first four decimals.
    Example: 67.1497

    I have tried with Trim, Right, Mid... but I cannot code it right.

  2. #2
    gibra
    Guest

    Re: Removing information from a string? VB6

    You should use InStr() function to get the comma position,
    next use Mid$() to retrieve the string at right of comma.
    Read help file for how to use this functions.

  3. #3
    Addicted Member
    Join Date
    Sep 2015
    Posts
    225

    Re: Removing information from a string? VB6

    Code:
    Dim arr() As String
    Dim tmp As String
    
    Open App.Path & "\File.txt" For Input As #1
    Open App.Path & "\NewFile.txt" For Output As #2
    While Not EOF(1)
        Line Input #1, tmp
        arr = Split(tmp, ",")
        Print #2, Round(arr(1), 4)
        DoEvents
    Wend
    Close #1
    Close #2
    MsgBox "Done!"

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Removing information from a string? VB6

    Use Instr to get the position of the comma (save it in a variable, e.g. commaPos)
    Use Instr to get the position of the period and add 4 to it (save it in a variable, e.g. lastPos)
    Subtract commaPos from lastPos to get the Length you want to pull.
    Use Mid$ to pull the string starting at commaPos + 1, and the length you calculated.

    If you're not sure there will always be four or more digits after the decimal point, you might want to check that lastPos is not greater than the length of the string, and if it is, set it to the length of the string before calculating the length you want to pull.

  5. #5
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Removing information from a string? VB6

    oops i thought you wanted all of the data - any way the process it the same just amend the output


    you can read the lines in as varA and varB

    and output varA and varB with separator=space

    ie
    Code:
    output=varA & " " & fn(varB)
    where the function trims var as required


    or work the whole line use replace to replace the comma

    Code:
    varB=12.667744
    use Format$(varB,"###0.0000") = 12.6677
    so line above would be


    Code:
    output=varA & " " & format$(varB,"###.0000")
    this handles 3 leading numbers and 4 decimals

    so add and remove # appropriately


    here to help

    remember to rate replies that help and close the thread when done
    Last edited by incidentals; May 26th, 2016 at 03:48 PM.

  6. #6
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: Removing information from a string? VB6

    Why wouldn't you just keep all the decimals and round it?

    Dim Val As Double
    Dim Line$
    Line = "2014.05.26 11:45:00,67.14975845410586"
    Val = Fix(CDbl(Split(Line$, ",")(1)) * 10000#) / 10000#

    https://support.microsoft.com/en-us/kb/196652

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Removing information from a string? VB6

    While split will work if that is a fixed position where the comma is then I would just use Mid$() to get either the 7 characters you want or everything after the comma and round it off to 4 places.

    If the comma may occur at different positions then I would use Instr() and Mid$() as that would be faster than using Split()

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Removing information from a string? VB6

    Quote Originally Posted by incidentals View Post
    oops i thought you wanted all of the data - any way the process it the same just amend the output
    Probably off on a tangeant there. In post #1 it was implied this is existing data taken from a file, to be "cleaned." Not data being created by the program.


    There could be other issues too. Might be lines with no comma. Might be spaces after the comma to be trimmed. Might be lots of things. All can be handled by tweaking the code:

    Name:  sshot1.png
Views: 1109
Size:  11.4 KB

    Before


    Name:  sshot2.png
Views: 1008
Size:  10.9 KB

    After


    Code:
    Option Explicit
    
    Private SuppressEvents As Boolean
    
    Private Sub cmdClean_Click()
        Dim Text As String
        Dim OutPos As Long
        Dim InPos As Long
        Dim CommaPos As Long
        Dim EndLinePos As Long
        Dim MoveLen As Long
        Dim ChunkToMove As String
    
        cmdClean.Enabled = False
        Text = Text1.Text
        OutPos = 1
        InPos = 1
        Do While InPos <= Len(Text)
            CommaPos = InStr(InPos, Text, ",")
            If CommaPos < 1 Then CommaPos = InPos - 1
            EndLinePos = InStr(InPos, Text, vbNewLine)
            If EndLinePos < 1 Then
                EndLinePos = Len(Text) + 1
            Else
                EndLinePos = EndLinePos + 2
            End If
            If CommaPos < EndLinePos Then InPos = CommaPos + 1
            MoveLen = EndLinePos - InPos
            'Remove leading spaces.  If not desired, remove
            'use of LTrim$() here:
            ChunkToMove = LTrim$(Mid$(Text, InPos, MoveLen))
            Mid$(Text, OutPos) = ChunkToMove
            InPos = InPos + MoveLen
            OutPos = OutPos + Len(ChunkToMove)
        Loop
        SuppressEvents = True
        Text1.Text = Left$(Text, OutPos - 1)
        SuppressEvents = False
    End Sub
    
    Private Sub Text1_Change()
        If SuppressEvents Then Exit Sub
        cmdClean.Enabled = True
    End Sub
    Zip, zam, zowie. Much quicker than "split-de-splits" or awful RegEx techniques, though not important unless large masses of data need to be cleaned.
    Attached Files Attached Files

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Removing information from a string? VB6

    Also, showing leading space removal:

    Name:  sshot3.png
Views: 1159
Size:  10.7 KB

    Add a comma


    Name:  sshot4.png
Views: 1074
Size:  10.7 KB

    After

  10. #10
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Removing information from a string? VB6

    op does not need to think about errant lines he states all lines are the same structure

    so read a,b

    will resolve the number with the decimal is b

    so format$(b,"###.0000") would be fine

    how the op uses these facts is upto them


    here to help

  11. #11
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Removing information from a string? VB6

    You might want to re-read the question. It isn't about output but modifying input.

  12. #12
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Removing information from a string? VB6

    the process of modifying a thing that comes in is to output the modification

    whether the change is made to a line in a file or a placeholder in memory the approach is the same

    the op needs to know that the line in the file can be read in as a and b where b is after the comma

    and that b can be modified using the format$ function...

    Now tell me how do you change the contents of a file so that it reflects the needs of the op without "outputing"/modifying something

    where are they putting/keeping the changed value/shape or B

    here to wonder if you are reading what i am writting?


    its about the OP learning some thing new

    (we should not be fighting each other here)

    here to be as helpful as possible

    what follows is an example of what the op could do using the technique i have suggested

    there is enough here to help the OP understand how data can be read and manipulated in vb6

    Code:
    Dim strEmpFileName As String
    Dim strBackSlash As String
    Dim intEmpFileNbr As Integer
     
    Dim strEmpName As String
    Dim intDeptNbr As Integer
    Dim strJobTitle As String
    Dim dtmHireDate As Date
    Dim sngHrlyRate As Single
     
    strBackSlash = IIf (Right$(App.Path, 1) = "\", "", "\")
    strEmpFileName = App.Path & strBackSlash & "EMPLOYEE.DAT"
    intEmpFileNbr = FreeFile
     
    Open strEmpFileName For Input As #intEmpFileNbr
     
    Do Until EOF(intEmpFileNbr)
    
    Input #intEmpFileNbr, strEmpName, intDeptNbr, strJobTitle, dtmHireDate, sngHrlyRate
    Print strEmpName; _
    
    Tab(25); Format$(intDeptNbr, "@@@@"); _
    
    Tab(35); strJobTitle; _
    
    Tab(55); Format$(dtmHireDate, "mm/dd/yyyy"); _
    
    Tab(70); Format$(Format$(sngHrlyRate, "Standard"), "@@@@@@@")
    
    Loop
    here to help
    Last edited by incidentals; May 27th, 2016 at 04:42 PM.

Tags for this Thread

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