I have a simple snippet (or so I thought) to truncate a sentence to a certain length, but keeping whole words.
For some reason the compiler says this statement is unreachable, but I can't see why. Extra set of eyes required...
Alwaysbusy
For some reason the compiler says this statement is unreachable, but I can't see why. Extra set of eyes required...
B4X:
public Sub Truncate(s As String, len As Int) As String 'ignore
If s.Length <= len Then
Return s
End If
Dim suffix As String = Chr(0x2026)
Dim words() As String = Regex.Split(" ", s)
Dim ret As String
Dim index As Int
' does not work:
'Do While True
' Solution by Erel
Do While Not(False)
If (ret.Length + words(index).Length) < len Then
ret = ret & words(index) & " "
index = index + 1
If index = words.Length Then
Return ret.trim & suffix
End If
Else
Return ret.trim & suffix ' <----- unreachable statement if one uses Do White True
End If
Loop
End Sub
Alwaysbusy
Last edited: