Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

string - fixed lenght

Posted on 2010-11-12
2
Medium Priority
?
342 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

885 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