views:

45

answers:

2

I want to break a string into chunks of 200 characters or smaller, but breaking at the spaces. How can I do this in VB.net? For example:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ut dui et sapien ultricies laoreet. Duis eleifend ante et tortor adipiscing tincidunt. Nulla sagittis purus sit amet mauris convallis auctor cursus orci volutpat. Nam id massa eu purus congue adipiscing. Nam a ligula in justo euismod fermentum eget et risus. Sed magna lectus, adipiscing in accumsan eu, porttitor id mauris. Suspendisse eros velit, ullamcorper elementum interdum et, tempor sed velit. Curabitur et velit purus, ut amet.

should be the starting string. Following this example, what I'm trying to extract is:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ut dui et sapien ultricies laoreet. Duis eleifend ante et tortor adipiscing tincidunt. Nulla sagittis purus sit amet mauris convallis

and

auctor cursus orci volutpat. Nam id massa eu purus congue adipiscing. Nam a ligula in justo euismod fermentum eget et risus. Sed magna lectus, adipiscing in accumsan eu, porttitor id mauris.

and

Suspendisse eros velit, ullamcorper elementum interdum et, tempor sed velit. Curabitur et velit purus, ut amet.

assuming no whitespace is left on the ends. How can I achieve this in vb.net?

+1  A: 

Here's the function:

Private Function split(ByVal textToSplit As String, ByVal charCount As Integer) As System.Collections.Queue

    Dim returnQueue As New System.Collections.Queue

    Dim words As String() = textToSplit.Split(" ".ToCharArray)

    Dim currentChunk As String = ""

    For index As Integer = 0 To words.GetUpperBound(0)

        Dim currentWord As String = words(index)

        If currentChunk.Length + currentWord.Length <= charCount Then
            'The phrase is still short enough
            currentChunk += " " & currentWord
        Else
            'The phrase would be too long
            'Add the chunk to the list
            returnQueue.Enqueue(currentChunk)
            'Start a new chunk
            currentChunk = currentWord
        End If

    Next index

    'Reached the end. Add the last chunk to the list
    returnQueue.Enqueue(currentChunk)

    Return returnQueue

End Function

Call it like this:

    Dim s As String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ut dui et sapien ultricies laoreet. Duis eleifend ante et tortor adipiscing tincidunt. Nulla sagittis purus sit amet mauris convallis auctor cursus orci volutpat. Nam id massa eu purus congue adipiscing. Nam a ligula in justo euismod fermentum eget et risus. Sed magna lectus, adipiscing in accumsan eu, porttitor id mauris. Suspendisse eros velit, ullamcorper elementum interdum et, tempor sed velit. Curabitur et velit purus, ut amet."

    For Each chunk As String In split(s, 200)
        Debug.WriteLine(chunk)
    Next
Michael Rodrigues
Great! Thanks so much!
Cyclone
Have a look at regular expressions: http://en.wikipedia.org/wiki/Regular_expressions They'll fundamentally change the way anyone works with strings. To match the given condition: `^.{0,200}\b` That's it -- "match any character from the start of the string, up to 200, but must end at a word break." In .NET, it is in `System.Text.RegularExpressions`
Jay
+2  A: 

This screams for a regular expression.

Imports System.Text.RegularExpressions

…

Function Split(ByVal source As String, ByVal chunkMaxLength As Integer) As IEnumerable(Of String)
    Dim index = 0
    Dim maxIndex = source.Length - 1
    Dim src = source
    Dim results As New List(Of String)

    Do While index < maxIndex
        src = source.Substring(index)
        Dim match = Regex.Match(src, "^.{0," & chunkMaxLength & "}\b").ToString()
        results.Add(match)
        index = index + match.Length
    Loop

    Return results

End Function
Jay