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
    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?

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

    How to write long string sentences

    Felipe Blin, Mar 16, 2004, in forum: Microsoft VB .NET
    Replies:
    5
    Views:
    852
    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:
    539
  3. Dave
    Replies:
    4
    Views:
    614
  4. kurt sune

    String.Split versus Strings.Split

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

    Regular expression to split a long string

    Kerry, Aug 17, 2005, in forum: Microsoft VB .NET
    Replies:
    2
    Views:
    607
  6. Mad Scientist Jr

    split a string into an arraylist?

    Mad Scientist Jr, Feb 15, 2006, in forum: Microsoft VB .NET
    Replies:
    2
    Views:
    5,524
    Phill W.
    Feb 16, 2006
  7. John

    Overloading string split function

    John, May 8, 2008, in forum: Microsoft VB .NET
    Replies:
    2
    Views:
    272
    Family Tree Mike
    May 10, 2008
  8. Terry Moreau

    chop, separate, split a STRING into sections?

    Terry Moreau, Dec 3, 2008, in forum: Microsoft VB .NET
    Replies:
    18
    Views:
    1,019
    Chris Dunaway
    Dec 4, 2008
Loading...