Android Code Snippet Random Text Generator

Discussion in 'Code Snippets' started by fredo, Apr 15, 2019.

Similar threads

B4A Code Snippet [B4X] GUID
B4A Class EscapeXml - Code snippet
B4A Tutorial Text files
B4A Tutorial TabHost tutorial
B4A Code Snippet Remove accents from string
B4A Code Snippet Get the APK signature at runtime
  1. fredo

    fredo Active 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

    Code:
    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(515' number of words
            For w = 0 To n
                
    If w Mod 3 = 0 Then
                    sb.Append(
    Chr(Rnd(6588))) 
                
    End If
                
    For l=4 To Rnd(515)
                    sb.Append(
    Chr(Rnd(97119)))
                
    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:
    Code:
    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(210)))
            sb.Append(
    "<br>")
        
    Next
        sb.Append(
    "<br>")
        sb.Append(
    "</body>")
        sb.Append(
    "</html>")
        
    Return sb.ToString
    End Sub
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice