Function to split long string into "sentences"




I have written a function to split a string into sub strings of a given
fixed max length. This is useful for example in breaking a long message into
multiple strings of up to 160 characters to be sent as individual SMS.

I am posting the function here in case its useful for someone.


'Function to split a long string into sentences of upto MaxLength size
Public Function Split(ByVal Expression As String, ByVal MaxLength As
Integer) As String()
Dim Separator As String = " "
Dim OutAr(0) As String
Dim j As Integer
Dim i As Integer

j = 0
While Expression <> ""
If Microsoft.VisualBasic.Len(Expression) <= MaxLength Then
ReDim Preserve OutAr(j)
OutAr(j) = Expression
Expression = ""
i = InStrRev(Microsoft.VisualBasic.Left(Expression,
MaxLength + 1), Separator)
If i = 0 Then
ReDim Preserve OutAr(j)
OutAr(j) = Microsoft.VisualBasic.Left(Expression,
Expression = Microsoft.VisualBasic.Right(Expression,
Len(Expression) - MaxLength)
j = j + 1
ReDim Preserve OutAr(j)
OutAr(j) = Microsoft.VisualBasic.Left(Expression, i - 1)
Expression = Microsoft.VisualBasic.Right(Expression,
Len(Expression) - i)
j = j + 1
End If
End If
End While

Split = OutAr
End Function

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question