• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

divide string equaly

I am pulling data from db and a lenght max is 5000. I need to put in it Excel but each line need to be 1200 only. I can write to Excel, no problem.
2 Solutions
Try this

Dim i as integer
Dim MyNewStr As String
For i = 0 to MyStr.Length - 1 Step 1200
    MyNewStr &= MyStr.SubString(i, 1200) & vbcrlf
MyNewStr &= MyStr.Substring(i, MyStr.Length - i)
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could also do something like:

    Dim BlockString As String = BreakString(DatabaseString, 1200)

Private Function BreakString(ByVal str As String, ByVal breakEvery As Integer) As String
        Dim sb As New System.Text.StringBuilder(str)
        Dim startAt As Integer = str.Length \ breakEvery ' <-- Integer division
        For i As Integer = startAt To 1 Step -1
            sb.Insert(i * breakEvery, vbCrLf)
        Return sb.ToString
    End Function

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now