Avatar of VBdotnet2005
VBdotnet2005
Flag for United States of America asked on

string - fixed lenght

I want to write to a file that each line contain up to 44 charactors and add a number to each line? The max of string is 2058.

Mystring = This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.

This is a test This is a test This is a test  - line 1
This is a test This is a test This is a test  - line 2
This is a test This is a test This is a test  - line 3
etc

dim mystring as string = mystring above
dim arr as new list(of string)
dim sr as new streamwriter(myfile)
For z as interger = 0 to mystring.lengh - 1 step 44
    while mystring.lenght < 2048
             
    arr.add ??? line # 1
    sr.write(mystring(up to 44) and line number
 end while       
next
Visual Basic.NET

Avatar of undefined
Last Comment
Shahan Ayyub

8/22/2022 - Mon
Shahan Ayyub

Please Check this:
Dim Mystring As String = "This is a test This is a test This is a test This is a test This is a test This is a test This is a test This is a test This is a test This is a test This is a test This is a test This is a test This is a test This is a test This is a test This is a test This is a test This is a test This is a test This is a test This is a test"
        Dim arr As New List(Of String)
        Dim sw As New StreamWriter("C:\SampleOutput.txt")
        Dim num As Integer = 0
        For i As Integer = 0 To Mystring.Length - 44 Step 44
            num = num + 1
            arr.Add(Mystring.Substring(i, 44) & " - " & " Line " & num)
        Next
        Dim joinedString As String = String.Join(vbCrLf, arr.ToArray)
        sw.Write(joinedString)
        sw.Close()

Open in new window

ASKER CERTIFIED SOLUTION
Shahan Ayyub

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck