Solved

string - fixed lenght

Posted on 2010-11-12
2
334 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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now