Results 1 to 3 of 3

Thread: Split String into multiple strings with a character limit w/ out splitting words

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2013
    Posts
    21

    Split String into multiple strings with a character limit w/ out splitting words

    So I have a piece of code that puts together a character string that splits the string into two strings when it exceeds 325 characters. Currently It will only work for 650 characters or less. I don't want to simply count the characters up to 325, 650, 975 etc. and merely split them. I want it to place the split at a "blank space" or the last blank space before the character limit so that the tech is readable.

    This is what I have that works for 650 characters or less. I am looking to split the character strings into multiple strings under 325 characters for up to 1625 characters. Here is the code I have so far for up to 650 characters.

    Code:
    'Splits between 325 & 650 characters
            If cnt > 325 Then
                Dim notessplit As New Form3
    
                ' Count through characters and mark last vbnewline
                Dim counterthree As Integer = 0
                For Each c As Char In clipcontents.ToString
                    counterthree = counterthree + 1
                    If counterthree < 325 Then
                        If c = " " Then countreturn = counterthree 'this counts based on spaces
                        ' If c = vbCr Then countreturn = counterthree ' use this to split based on character returns
                    End If
                Next
    
                'Split into two different clip contents
                Dim countertwo As Integer = 0
                For Each c As Char In clipcontents.ToString
                    countertwo = countertwo + 1
                    If countertwo < countreturn Then
                        tempstring.Append(c)
                    End If
    
                    If countertwo = countreturn Then
                        FirstHalfClipContents = (tempstring.ToString)
                    End If
    
                    If countertwo > countreturn Then 'halfcnt - 1 Then
                        tempstring2.Append(c)
                        SecondHalfClipContents = (tempstring2.ToString)
                    End If
                Next
                ' Set the Parent Form of the Child window. 
                'NotesSplit.MdiParent = Me
                notessplit.FirstHalfClipContents = FirstHalfClipContents
                notessplit.SecondHalfClipContents = SecondHalfClipContents
                notessplit.clipcontents = clipcontents.ToString
                ' Display the new form. 
                notessplit.Show()
            Else
                My.Computer.Clipboard.SetText(clipcontents.ToString)
            End If

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: Split String into multiple strings with a character limit w/ out splitting words

    I haven't actually tested this but it seems like it should work:
    vb.net Code:
    1. ''' <summary>
    2. ''' Splits a string into multiple substrings of a maximum length without breaking words.
    3. ''' </summary>
    4. ''' <param name="text">
    5. ''' The string to split.
    6. ''' </param>
    7. ''' <param name="maxLength">
    8. ''' The maximum length of each substring.
    9. ''' </param>
    10. ''' <returns>
    11. ''' An array of substrings.
    12. ''' </returns>
    13. Private Function Split(text As String, Optional maxLength As Integer = 325) As String()
    14.     Dim substrings As New List(Of String)
    15.  
    16.     Do Until text.Length = 0
    17.         If text.Length <= maxLength Then
    18.             'There is only one substring left.
    19.             substrings.Add(text)
    20.             text = String.Empty
    21.         Else
    22.             Dim length = maxLength
    23.  
    24.             'Find the index at or before the maxLength that there is not a letter on both sides of the split.
    25.             Do While Char.IsLetter(text(length)) AndAlso Char.IsLetter(text(length - 1))
    26.                 length -= 1
    27.             Loop
    28.  
    29.             substrings.Add(text.Substring(0, length))
    30.             text = text.Substring(length)
    31.         End If
    32.     Loop
    33.  
    34.     Return substrings.ToArray()
    35. End Function

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2013
    Posts
    21

    Re: Split String into multiple strings with a character limit w/ out splitting words

    Awesome - That worked perfectly. Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width