Function to split long string into "sentences"

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

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.

Regards


'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 = ""
Else
i = InStrRev(Microsoft.VisualBasic.Left(Expression,
MaxLength + 1), Separator)
If i = 0 Then
ReDim Preserve OutAr(j)
OutAr(j) = Microsoft.VisualBasic.Left(Expression,
MaxLength)
Expression = Microsoft.VisualBasic.Right(Expression,
Len(Expression) - MaxLength)
j = j + 1
Else
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
 
Back
Top