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.
Similar Threads
  1. Felipe Blin

    How to write long string sentences

    Felipe Blin, Mar 16, 2004, in forum: Microsoft VB .NET
    Replies:
    5
    Views:
    718
    Jay B. Harlow [MVP - Outlook]
    Mar 16, 2004
  2. jack

    Need to split a String into an ArrayList

    jack, Apr 29, 2004, in forum: Microsoft VB .NET
    Replies:
    2
    Views:
    422
  3. kurt sune

    String.Split versus Strings.Split

    kurt sune, Mar 30, 2005, in forum: Microsoft VB .NET
    Replies:
    5
    Views:
    872
    Phill. W
    Mar 30, 2005
  4. Kerry

    Regular expression to split a long string

    Kerry, Aug 17, 2005, in forum: Microsoft VB .NET
    Replies:
    2
    Views:
    503
  5. John

    Overloading string split function

    John, May 8, 2008, in forum: Microsoft VB .NET
    Replies:
    2
    Views:
    226
    Family Tree Mike
    May 10, 2008
Loading...

Share This Page