将一个长字符串分成两个空格并将每个空格输出为新行

Split a long string by two spaces and output each as new lines

我有一个像

这样的字符串
Dim theText As String = "Visual Basic is a legacy third generation event driven programming language and integrated development environment (IDE) from Microsoft."

现在我想将字符串分成两个空格,并将每个空格输出为一个新行。比如像

Visual Basic
is a
legacy third
generation event
driven programming
language and
integrated development
environment (IDE)
from Microsoft.

我尝试使用以下代码来解决这个问题,但似乎并没有像我需要的那样工作。

Dim LineLength As Integer = 3
    Dim currPos As Integer
    Dim theText As String = "Visual Basic is a legacy third generation event driven programming language and integrated development environment (IDE) from Microsoft."
    Dim thisLine As String
    Dim allLines As New StringBuilder()
    While theText.Length > LineLength
        currPos = theText.IndexOf(" ", LineLength)
        If currPos > -1 Then
            thisLine = theText.Substring(0, currPos + 1)
            theText = theText.Remove(0, currPos + 1)
            allLines.Append(thisLine)
            allLines.Append(vbCrLf)
        End If
    End While
    allLines.Append(theText)
    TextBox2.Text = allLines.ToString()

有什么方法可以实现吗?

使用 .Split 和 .Join 方法给出:

    Dim theText As String = "Visual Basic  is a  legacy third  generation event  driven programming  language and  integrated development  environment (IDE)  from Microsoft."

    Dim parts() As String = theText.Split(New String() {"  "}, StringSplitOptions.None)
    TextBox2.Text = String.Join(Environment.NewLine, parts)

复制/粘贴以上内容时请小心。双 space 可能会在文本中转换为单个 space。

编辑:如果意图是打破彼此 space 那么:

    Dim theText As String = "Visual Basic is a legacy third generation event driven programming language and integrated development environment (IDE) from Microsoft."
    Dim parts() As String = theText.Split(New Char() {" "c})

    Dim sb As New System.Text.StringBuilder
    For x As Integer = 1 To parts.Length - 1 Step 2
        sb.AppendFormat("{0} {1}{2}", parts(x - 1), parts(x), Environment.NewLine)
    Next
    If parts.Length Mod 2 = 1 Then sb.AppendFormat("{0}{1}", parts(parts.Length - 1), Environment.NewLine)

作为函数:

Public Function StringToLines(theString As String, _
                              Optional wordsPerLine As Integer = 3, _
                              Optional splitOn As Char() = Nothing) As String

    Dim spltChrs As Char() = New Char() {" "c}
    If splitOn IsNot Nothing Then
        spltChrs = splitOn
    End If

    Dim words() As String = theString.Split(spltChrs, StringSplitOptions.RemoveEmptyEntries)

    Dim sb As New System.Text.StringBuilder
    Dim lines As Integer = 0

    For Each w As String In words
        sb.Append(w)
        sb.Append(" ")
        lines += 1
        If lines >= wordsPerLine Then
            sb.AppendLine()
            lines = 0
        End If
    Next

    Do While sb(sb.Length - 1) = ControlChars.Cr OrElse sb(sb.Length - 1) = ControlChars.Lf OrElse sb(sb.Length - 1) = " "
        sb.Remove(sb.Length - 1, 1)
    Loop
    Return sb.ToString
End Function

原回答:

    Dim inputstr As String = "Visual Basic is a legacy third generation event driven programming language and integrated development environment (IDE) from Microsoft."
    Dim inarray As String() = inputstr.Split(" ")
    Dim outarray As New List(Of String)
    Dim maxiteration As Integer = inarray.Length - 1
    For i As Integer = 0 To maxiteration Step 2
        If i = maxiteration Then
            outarray.Add(inarray(i)) 'if words count is odd
        Else
            outarray.Add(inarray(i) & " " & inarray(i + 1))
        End If
    Next
    Dim outstr = String.Join(vbCrLf, outarray)

改进后的答案:

Function StringBreak(ByVal input As String, Optional ByVal wordsPerRow As Integer = 2) As String
    Dim inarray As String() = input.Split(" ")
    Dim outstr As String = inarray(0)
    For i As Integer = 1 To inarray.Length - 1
        If i Mod wordsPerRow = 0 Then
            outstr &= vbCrLf & inarray(i)
        Else
            outstr &= " " & inarray(i)
        End If
    Next
    Return outstr
End Function

Dim result = StringBreak("Visual Basic is a legacy third generation event driven programming language and integrated development environment (IDE) from Microsoft.", 3)

使用最后一个参数设置每行的字数。

Dim count As Integer = 0
Dim Text As String = "Visual Basic  is a  legacy third  generation event  driven programming  language and  integrated development  environment (IDE)  from Microsoft."
Dim words As String() = Regex.Split(Text, " ")
For Each word As String In words
    count = count + 1
    Response.Write(word & " ")
    If count = 2 Then
        Response.Write("</br>")
        count = 0

    End If
Next
' original poster's original string. 
' note, SINGLE SPACES were specified, not double spaces.
Dim theText As String = "Visual Basic is a legacy third generation event driven programming language and integrated development environment (IDE) from Microsoft."

' regular expression built using Expresso
' will match a word plus a space plus a word, then break on the second space encountered

'  Regular expression built for Visual Basic on: Sat, Jun 27, 2015, 11:34:11 AM
'  Using Expresso Version: 3.0.4750, http://www.ultrapico.com
'  
'  A description of the regular expression:
'  
'  [1]: A numbered capture group. [[^ ]+ [^ ]+]
'      [^ ]+ [^ ]+
'          Any character that is NOT in this class: [ ], one or more repetitions
'          Space
'          Any character that is NOT in this class: [ ], one or more repetitions
'  Space, zero or one repetitions
'  
'

Dim regex As Regex = New Regex( _
      "([^ ]+ [^ ]+) ?", _
    RegexOptions.CultureInvariant _
    Or RegexOptions.Compiled _
    )


' match the string against the regex

Dim matches = regex.Matches(theText)

' The first capture group contains the matched string without the second space.
Dim lines = (From m In matches Select m.Groups(1).Value).ToList()

这是在 LINQPad 4 中执行的程序的屏幕截图,显示输出是每个字符串中的两个单词,如所要求的: