根据设定的字符长度拆分字符串

时间:2011-11-02 19:23:43

标签: vb.net asp.net-mvc-3 itextsharp

MVC 3. Vb.net。我的部分应用使用Itextsharp生成PDF文件。有些字符串太长,无法正确显示背景图像。所以我基本上需要在超过26个字符长时分割这个字符串,当它分裂时它不能在一个单词的中间分割。从那里我将使用换行将字符串添加到下一行的右侧...任何可能指向正确方向的想法..我确实开始构建函数,我将把字符串传递给测试长度然后完成之后将字符串传回,但之后我就陷入困境......

    Private Function stringLength(ByVal _string As String) As String
        If _string.Length < 26 Then
            _string.Split(
        End If
    End Function

3 个答案:

答案 0 :(得分:1)

听起来你要求自动换行功能。

由于我觉得以一种促进学习的方式回答比回答问题更好,我给你一个链接,引导你完成使用测试驱动开发(TDD)来解决这个问题的过程。事实上,自动换行问题是一个流行的编码kata,罗伯特C.马丁写了一个有点愚蠢的虚构故事,关于开发人员正在教授如何使用TDD来解决自动换行。

代码示例使用Java,但读取和翻译应该是微不足道的。

http://thecleancoder.blogspot.com/2010/10/craftsman-62-dark-path.html

愚蠢的比特是可以跳过的。只需跳到第一个代码段之前的句子。

答案 1 :(得分:1)

我确信有一百万种不同的方法可以做到这一点。

您基本上需要将按空格分割的所有单词放入列表中。之后,您只需要继续检查当前单词加空格加下一个单词是否达到您的阈值,如果是,则移动到下一行。获得所有行后,再次将列表重新加入单个字符串。

Private Function LimitWidth(ByVal text As String, ByVal maxCharacters As Integer) As String
  Dim words As List(Of String) = text.Split(" "c).ToList()

  If text.Length < maxCharacters OrElse words.Count = 1 Then
    Return text
  Else
    Dim lines As New List(Of String)

    Dim currentLine As String = words(0)
    For i As Integer = 1 To words.Count - 1
      If (currentLine & " " & words(i)).Length > maxCharacters Then
        lines.Add(currentLine)
        currentLine = words(i)

        If i = words.Count - 1 Then
          lines.Add(currentLine)
        End If
      Else
        If i = words.Count - 1 Then
          lines.Add(currentLine & " " & words(i))
        End If

        currentLine &= " " & words(i)
      End If
    Next

    Return String.Join(Environment.NewLine, lines.ToArray())
  End If
End Function

测试:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
  MessageBox.Show(LimitWidth("This is a really long sentence " & _
                             "meant to demonstrate how to split " & _
                             "the words into a confined character length.", 26))
End Sub

答案 2 :(得分:0)

我将通过以下方式添加对多行输入文本的处理:

Private Function LimitWidth(ByVal text As String, ByVal maxCharacters As Integer, SplitSign As String) As String
    Dim Output As String = ""
    Dim OrgLines As List(Of String) = text.Split(Environment.NewLine).ToList()
    For x As Integer = 1 To OrgLines.Count - 1
        Dim words As List(Of String) = OrgLines(x).Split(" "c).ToList()
        If text.Length < maxCharacters OrElse words.Count = 1 Then
            Output += OrgLines(x)
        Else
            Dim lines As New List(Of String)

            Dim currentLine As String = words(0)
            For i As Integer = 1 To words.Count - 1
                If (currentLine & " " & words(i)).Length > maxCharacters Then
                    lines.Add(currentLine)
                    currentLine = words(i)

                    If i = words.Count - 1 Then
                        lines.Add(currentLine)
                    End If
                Else
                    If i = words.Count - 1 Then
                        lines.Add(currentLine & " " & words(i))
                    End If

                    currentLine &= " " & words(i)
                End If
            Next
            Output += String.Join(SplitSign, lines.ToArray())
        End If
    Next
    Return Output
End Function

使用:

LimitWidth(“ your text”,80,Environment.NewLine)