infinite loop

magmo

Member
Joined
Oct 19, 2006
Messages
14
Programming Experience
1-3
Hi

I have a little problem with a "Do" loop. What I try to build is a function that works the same way as a textbox in for example indesign does. If you add a textbox there and the add some text that doesnt fit in the first lie it automatically add a "-" sign as a last charachter on that line and then continue with the text on the second line and so on.

Here's what I got so far..

HTML:
Function MakeMeChoppedString(ByVal MyString As String, ByVal ReplaceWith As String, ByVal iLength As Integer) As String
MyString = MyString & ("¤")
Dim MyTempString1 As String = MyString
Dim MyTempString2 As String = MyString
Dim MyTempString3 As String = MyString
Dim MyTempString4 As String = String.Empty
If MyString.Length > (iLength + 1) Then
Do Until (MyTempString2.Length + iLength) < (iLength + 1)
MyTempString3 = MyTempString2
If MyTempString3.Length > iLength Then
If MyTempString3.Substring(MyTempString3.Length - 1) = (" ") Then
MyTempString1 = MyTempString3.Substring(0, (iLength))
Else
If InStr(MyTempString3, " ") Then
MyTempString1 = MyTempString3.Substring(0, (iLength))
MyTempString1 = MyTempString1.Substring(0, InStrRev(MyTempString1, " ") - 1)
Else
Response.Write("heres where my problem starts...")
MyTempString1 = MyTempString1.Substring(0, iLength) & ("-")
iLength = iLength + 1
End If
End If
Else
MyTempString1 = MyTempString3
End If
MyTempString2 = MyTempString3.Remove(0, MyTempString1.Length)
MyTempString4 += MyTempString1 & ("¤")
Loop
End If
Return MyTempString4.Replace("¤", ReplaceWith).ToString
End Function
 
 
Literal1.Text = (MakeMeChoppedString("This is my very long string and I must say its waaaaaaaaaaaaaaaaaaaaaaytolong", ("<br>"), 20))

I would like my function to return a string like this..

This is my very long
string and I must
say its waaaaaaaaaa-
aaaaaaaaaaaaytolong


I realize that the loop some how is infinite and I probably must change a length or something, but I can't figure it out. Can someone please take a look and check what might be wrong?


Regards
 
A different approach.. I split the string by spaces, then iterate and process each "word" according to the formatting rules. I added one rule myself, that is not chopping short words (up to 6 chars)
VB.NET:
Private Sub testtextformatting()
    Dim f As New formatting
    f.linelimit = 20
    f.delimiter = "-"
    f.linefeed = "<br />" & vbNewLine
    Dim text As String = "This is my very long string and I must say its waaaaaaaaaaaaaaaaaaaaaaytolong"
    MsgBox(formattedtext(text, f))
End Sub
 
Private Class formatting
    Public delimiter, linefeed As String, linelimit As Integer
End Class
 
Private Function formattedtext(ByVal text As String, ByVal format As formatting) As String
    Dim split() As String = text.Split(" ")
    Dim newtext As New System.Text.StringBuilder
    Dim currentline As String = ""
    For Each word As String In split
        [COLOR=darkgreen]'first finish up after last word[/COLOR]
        If currentline.Length = format.linelimit Then [COLOR=darkgreen]'perfect line[/COLOR]
            newtext.Append(currentline & format.linefeed)
            currentline = ""
        ElseIf currentline <> "" Then
            currentline &= " "
        End If
        [COLOR=darkgreen]'process current word[/COLOR]
        If currentline.Length + word.Length <= format.linelimit Then [COLOR=darkgreen]'fits[/COLOR]
            currentline &= word
        ElseIf word.Length <= 5 Then [COLOR=darkgreen]'don't chop short words, transfer[/COLOR]
            newtext.Append(currentline & format.linefeed)
            currentline = word
        Else [COLOR=darkgreen]'needs unknown amount of chopping[/COLOR]
            currentline = chop(newtext, currentline & word, format)
        End If
    Next
    newtext.Append(currentline)
    Return newtext.ToString
End Function
 
[COLOR=darkgreen]'function returns remainder after some chopping, full "lines" are added directly to Builder[/COLOR]
Private Function chop(ByRef newtext As StringBuilder, ByVal input As String, ByVal format As formatting) As String
    If input.Length > format.linelimit Then
        newtext.Append(input.Substring(0, format.linelimit) & format.delimiter & format.linefeed)
        input = chop(newtext, input.Substring(format.linelimit), format)
    End If
    Return input
End Function
 
Hi John

Thats a really nice way to do it and it works just the way I wanted it to, Thank you very much for your help! You saved my day ;) .


Best Regards
 
Back
Top