-
Sep 30th, 2014, 08:03 PM
#1
Thread Starter
Junior Member
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
-
Sep 30th, 2014, 08:14 PM
#2
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:
''' <summary> ''' Splits a string into multiple substrings of a maximum length without breaking words. ''' </summary> ''' <param name="text"> ''' The string to split. ''' </param> ''' <param name="maxLength"> ''' The maximum length of each substring. ''' </param> ''' <returns> ''' An array of substrings. ''' </returns> Private Function Split(text As String, Optional maxLength As Integer = 325) As String() Dim substrings As New List(Of String) Do Until text.Length = 0 If text.Length <= maxLength Then 'There is only one substring left. substrings.Add(text) text = String.Empty Else Dim length = maxLength 'Find the index at or before the maxLength that there is not a letter on both sides of the split. Do While Char.IsLetter(text(length)) AndAlso Char.IsLetter(text(length - 1)) length -= 1 Loop substrings.Add(text.Substring(0, length)) text = text.Substring(length) End If Loop Return substrings.ToArray() End Function
-
Sep 30th, 2014, 10:01 PM
#3
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|