在VB.net中智能地拆分字符串

时间:2010-08-02 02:03:56

标签: vb.net string

我想将一个字符串分成200个字符或更小的字符块,但是要在空格处打破。我怎么能在VB.net中这样做?例如:

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.

应该是起始字符串。在这个例子之后,我想要提取的是:

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.

假设两端没有留下空白。我怎样才能在vb.net中实现这个目标?

2 个答案:

答案 0 :(得分:3)

这是功能:

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

这样称呼:

    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

答案 1 :(得分:3)

这个尖叫表示正则表达式。

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