Android Code Snippet Random Text Generator

Similar threads

B4A Code Snippet [B4X] GUID
B4A Class EscapeXml - Code snippet
B4A Tutorial Text files
B4A Tutorial TabHost tutorial
B4A Code Snippet [B4X] UrlEncodeMap
B4A Code Snippet Remove accents from string

fredo

Well-Known Member
Licensed User
Code to generate placeholder texts as an alternative to LoremIpsum.
Multiple paragraphs of different and arbitrary length are easy to create.

2019-04-15_07-17-34.jpg

B4X:
Sub RandomText(NumberOfSentences As Int) As String
    Dim n As Int = 0
    Dim sb As StringBuilder
    sb.Initialize
    For s = 1 To NumberOfSentences
        n = Rnd(5, 15) ' number of words
        For w = 0 To n
            If w Mod 3 = 0 Then
                sb.Append(Chr(Rnd(65, 88))) 
            End If
            For l=4 To Rnd(5, 15)
                sb.Append(Chr(Rnd(97, 119)))
            Next
            If w <> n Then sb.Append(" ")
        Next
        sb.Append(". ")
    Next
    Return sb.ToString
End Sub


An application example to create some natural looking text for a webview:
B4X:
Sub CreateRandomHtmlText(NumberOfParagraphs As Int) As String
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append("<html>")
    sb.Append("<body>")
    sb.Append("<h2>").Append("Xyz Header").Append("</h2>")
    For i = 1 To NumberOfParagraphs
        sb.Append("<h4>").Append("Testparagraph ").append(i).Append("</h4>")
        sb.Append(RandomText(Rnd(2, 10)))
        sb.Append("<br>")
    Next
    sb.Append("<br>")
    sb.Append("</body>")
    sb.Append("</html>")
    Return sb.ToString
End Sub
 
Top