Link to home
Create AccountLog in
Avatar of VBdotnet2005
VBdotnet2005Flag 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
Avatar of Shahan Ayyub
Shahan Ayyub
Flag of Pakistan image

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
Avatar of Shahan Ayyub
Shahan Ayyub
Flag of Pakistan image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer