Solved

string - fixed lenght

Posted on 2010-11-12
2
337 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:VBdotnet2005
  • 2
2 Comments
 
LVL 19

Expert Comment

by:Shahan Ayyub
ID: 34124684
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

0
 
LVL 19

Accepted Solution

by:
Shahan Ayyub earned 125 total points
ID: 34132616
Hi!

I didn't see that you requirement need another condition that MyString should be "The max of string is 2058".

So check this as well.
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
If Mystring.Length <= 2058 Then
   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()
End If

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question