Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7987947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:17:40+00:00 2026-06-04T12:17:40+00:00

I am reading a text file with mostly alpha characters. the content is not

  • 0

I am reading a text file with mostly alpha characters. the content is not really relevant but the size of each line is very important. The process I will feed this text to will require each line be no more than 50 characters. So I will pre-process the text and add line feeds to make sure that happens.

I tried several VB.NET regex like ^.*$ but that doesn’t really break up the lines by 50 characters. I would take the result and iterate through each match and then cut it up and write it to an object in memory. Can this be done with a single regex pass?

Otherwise I will use a streamreader and on each line check the length and if <=50 write it out with a streamwriter. if >50 cut it up in sections of 50 and then use streamwriter.

A brief example of my text:

 119  SMITH KATY AAAA  F   ZZZ     X NB SX ET
          MILES,200/LM450                       
 120  JONES  THOMAS      W   QQQ  66-W NB OS SC LW EP       
                                              ET
          L/G/B/MAY20-2010/JONES/THOMAS/KEITH      121  BUBBA BILLY  HH4  S   XQT 2PA-F  1 IP SC LH ET
                                              DOCC
 122  NEWTON   IAASAC      S   FTY 240-U NB QC LF KD EE

Just looking for tips on how to efficiently do this.

Update: I ended up using the streamreader approach as suggested by SSS. However, I tried to avoid the old Mid function and stick with Substring. Thus i had to make some checks and use some code from another SO post but cant remember which one. anyway here it is:

    Dim reader As New StringReader(aSource)
    Dim line As String = Nothing
    Dim writer As New StringWriter
    Dim chunkSize As Integer = 50
    Dim chunk As String

    Do
        line = reader.ReadLine()
        If Not String.IsNullOrEmpty(line) Then
            Debug.WriteLine(line.Length & "-->" & line)
            'if line length is less than or equal to chunk size then write it out, otherwise cut it up and then write the chunks out
            If line.Length <= chunkSize Then
                writer.WriteLine(line)
            Else
                Debug.WriteLine("---------------------")
                For i = 0 To line.Length Step chunkSize
                    Debug.WriteLine("i  =" & i)
                    Debug.WriteLine("i+c=" & i + chunkSize)
                    Debug.WriteLine("L  =" & line.Length)
                    If i + chunkSize > line.Length Then
                        chunk = line.Substring(i, line.Length - i)
                    Else
                        chunk = line.Substring(i, chunkSize)
                    End If
                    Debug.WriteLine("  " & chunk.Length & "-->" & chunk)
                    writer.WriteLine(chunk)
                Next i
                Debug.WriteLine("---------------------")
            End If
        End If
    Loop While (line IsNot Nothing)
    reader.Close()
    reader.Dispose()

    'this cut string now becomes our source
    Debug.WriteLine("==>" & writer.ToString)
    sourceText = writer.ToString

    writer.Close()
    writer.Dispose()

Hope that helps someone with the same problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:17:42+00:00Added an answer on June 4, 2026 at 12:17 pm
    Imports System.IO
    Public Class Form1
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim strFilenameIn As String = "C:\Junk\Input.TXT"
        Dim strFilenameOut As String = "C:\Junk\Output.TXT"
        Try
          Using sr As New StreamReader(strFilenameIn)
            Using sw As New StreamWriter(strFilenameOut)
              Do
                Dim strLine As String = sr.ReadLine()
                If strLine Is Nothing Then Exit Do 'EOF
                For i As Integer = 1 To strLine.Length Step 50
                  sw.WriteLine(Mid(strLine, i, 50))
                Next i
              Loop
            End Using
          End Using
        Catch ex As Exception
          MsgBox(ex.Message)
          Exit Sub
        End Try
        MsgBox(strfilenameout & " written")
      End Sub
    End Class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here I'm reading text file which contains a integer in each line and I'm
I am reading a text file representing a maze pattern. each line is read
I am reading a text file line by line in C++. I'm using this
I am reading a text file and processing some records, a relevant sample of
I'm reading a text file line by line, and storing it into a NSArray.
I am reading a text file which I know its 38th line is Uncalibrated
I generate the XML by reading the text form .txt file. But I got
I'm reading a text file line by line and converting it into a string.
I have a simple Excel 2007 Macro that is reading a text file line
So I am reading a text file which may or may not end with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.