[Gambas-user] Text-Wrapping

Christof Thalhofer chrisml at deganius.de
Wed Nov 11 14:02:17 CET 2020


Hi Hans,

> Does anybody have a finished function in his source code box?
Please try this:

------------------------------------------------------------------------

Public Function Wrap(TextToWrap As String, Count As Integer) As String

    Dim sCurWord, sRow As String
    Dim Blocks, Words, Rows As String[]
    Dim i, j As Integer

    Rows = New String[]

    If InStr(TextToWrap, gb.lf) > 0 Then
        Blocks = Split(TextToWrap, gb.lf, Null, False, True)
    Else
        Blocks = New String[]
        Blocks.Add(TextToWrap)
    Endif

    For Each TextToWrap In Blocks

        Words = Split(TextToWrap, " ")
        For i = 0 To Words.Count - 1

            'If TrimWord Like "*nachher" Then Stop
            sCurWord = Words[i]
            If Len(sRow) = 0 Then
                sRow = sCurWord
            Else If Len(sRow & " " & sCurWord) <= Count Then
                sRow &= " " & sCurWord
            Else
                sRow = sCurWord
            End If

            j = i + 1
            If j < Words.Count Then
                sCurWord = Words[j]
                If Len(sRow & " " & sCurWord) > Count Then
                    Rows.Add(sRow & gb.lf)
                    sRow = Null
                Endif
            Else If j = Words.Count Then
                Rows.Add(sRow)
                sRow = Null
            Endif

        Next
        Rows[Rows.Count - 1] &= gb.Lf
    Next

    Return Rows.Join("")

End
------------------------------------------------------------------------

Alles Gute

Christof Thalhofer

-- 
Dies ist keine Signatur

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20201111/b695188c/attachment.sig>


More information about the User mailing list