Function to split long string into "sentences"

Discussion in 'Microsoft VB .NET' started by John, May 8, 2008.

  1. John

    John Guest

    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
     
    John, May 8, 2008
    #1
    1. Advertisements

  2. John

    Rothco

    Joined:
    Jun 16, 2010
    Messages:
    3
    Likes Received:
    0
    Many thanks - very useful

    Roy
     
    Rothco, Aug 1, 2012
    #2
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page