Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split long strings into shorter ones 1

Status
Not open for further replies.

patriciaxxx

Programmer
Jan 30, 2012
277
GB

I have a text file which will contain at any one time, one string off varying length in the following style:

D0 CF 11 E0 A1 B1 1A E1 00 00 00 00 00 00 00

Basically always groups of any 2 characters separated by a space, and always one row no matter how long the string was.

I need to separate the string into substrings (separate rows) of a length I choose and in the following style.

"D0 CF 11 E0 " & _
"A1 B1 1A E1 " & _
"00 00 00 00 " & _
"00 00 00 "

The above example would be the desired result if I had chose a string length of 4.

The below example would be the result if I had selected a string length of 7.

"D0 CF 11 E0 A1 B1 1A" & _
"E1 00 00 00 00 00 00" & _
"00"

Then write the results to a new text file.

This is my code so far, it reads the file but that’s it. I have tried using the split function but couldn’t get it to do what I needed.

Code:
Sub ReadLines()
    
    Dim sInput As String
    Dim i As Long

    Open "C:\test.txt" For Input As #1

    Do While Not EOF(1)
        Input #1, sInput
        Debug.Print sInput
    Loop

End Sub

Any help would be much appreciated.

 
hi,

Check out the MID() function.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Thank you SkipVought

Taking your advice onboard I now have code looking like this, however it just produces the header and tail.

Any pointers as to if the code is how it should be, together with whatever I’m missing to make it work properly would be appreciated.

Code:
Sub ReadLines()
    Dim strSource As String
    Dim i As Long
    Dim intSize As Integer
    intSize = 16
    
    Dim sInput As String

    Open CurrentProject.Path & "\test.txt" For Input As #1

    Do While Not EOF(1)
        Input #1, sInput
        Debug.Print sInput
    Loop
    
    Dim strHead As String
    Dim strTail As String
    strHead = """"
    strTail = """ & _"
    
    With CreateObject("Scripting.FileSystemObject").CreateTextFile(CurrentProject.Path & "\output.txt", True)
        For i = 1 To Len(sInput) Step intSize
            .WriteLine strHead & Mid(strSource, i, intSize) & strTail
        Next
    End With
    
End Sub
 
My bad, I found a silly error in my code.
It now seems to work, so thank you SkipVought for pointer to mid function.
 
Well, if you are happy with the filesystemobject, as you appear to be, that might be rewritten as:

Code:
[blue][green]' Break input text into strings containing intSize atoms, where each atom is AtomSize characters[/green]
Sub ReadLines(intSize As Integer, Optional AtomSize As Long = 3)
    Dim i As Long
    Dim sInput As String
    
    sInput = CreateObject("Scripting.FileSystemObject").OpenTextFile(App.Path & "\input.txt").ReadAll
    With CreateObject("Scripting.FileSystemObject").CreateTextFile(App.Path & "\output.txt", True)
        For i = 1 To Len(sInput) Step intSize * AtomSize
            .WriteLine Trim$(Mid$(sInput, i, intSize))
        Next
    End With
End Sub[/blue]
 
Thank you strongm

I have one problem with my code which I can’t work out. It’s to do with the ‘strtail’ (it would also apply to your code as it hasn’t got the ‘strHeader’ and ‘strTail’)

I posted the following as an example desired result:

"D0 CF 11 E0 A1 B1 1A" & _
"E1 00 00 00 00 00 00" & _
"00"

However the input string example for the above and my code give the following result:

"D0 CF 11 E0 A1 B1 1A" & _
"E1 00 00 00 00 00 00" & _
"00" [highlight #FCE94F]& _[/highlight]

I don’t want the characters in yellow at the end of the last line.
How do I achieve this?
 
Actually, that give me a chance to correct a minor bug in my code
Code:
[blue][green]' Break input text into strings containing intSize atoms, where each atom is AtomSize characters[/green]
Sub ReadLines(intSize As Integer, Optional AtomSize As Long = 3)
    Dim i As Long
    Dim sInput As String
    
    sInput = CreateObject("Scripting.FileSystemObject").OpenTextFile(App.Path & "\input.txt").ReadAll
    With CreateObject("Scripting.FileSystemObject").CreateTextFile(App.Path & "\output.txt", True)
        For i = 1 To Len(sInput) - intSize * AtomSize Step intSize * AtomSize
            .WriteLine """" & Trim$(Mid$(sInput, i, intSize * AtomSize)) & """ &_"
        Next
        .WriteLine """" & Trim$(Mid$(sInput, i, intSize * AtomSize)) & """"
    End With
End Sub[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top