B4J Code Snippet WordsToProperCase

B4X:
Sub AppStart (Args() As String)
    Log(WordsToProperCase("RINGGIT   MALAYSIA ONe   thouSAND     anD  Five hundRed")) ' Ringgit Malaysia One Thousand And Five Hundred
End Sub

Sub WordsToProperCase (Words As String) As String
    If Words.Length < 1 Then Return Words
    Do While Words.Contains("  ")
        Words = Words.Replace("  ", " ")
    Loop
    Dim SB As StringBuilder
    SB.Initialize
    Dim Word() As String = Regex.Split(" ", Words)
    For i = 0 To Word.Length - 1
        If SB.Length > 0 Then SB.Append(" ")
        SB.Append(Word(i).SubString2(0, 1).ToUpperCase)
        If Word(i).Length > 1 Then SB.Append(Word(i).SubString(1).ToLowerCase)
    Next
    Return SB.ToString
End Sub
 

xulihang

Active Member
Licensed User
Longtime User
Where is it used for? To correct OCR results?
 

aeric

Expert
Licensed User
Longtime User
In my POS system, I use this function during PDF report generation.

1653582195314.png
 

aeric

Expert
Licensed User
Longtime User
B4X:
Sub WordsToProperCase (Words As String) As String
    If Words.Length < 1 Then Return Words
    Words = Regex.Replace("\h+", Words, " ")
    Dim SB As StringBuilder
    SB.Initialize
    Dim Word() As String = Regex.Split(" ", Words)
    For i = 0 To Word.Length - 1
        If SB.Length > 0 Then SB.Append(" ")
        SB.Append(Word(i).SubString2(0, 1).ToUpperCase)
        If Word(i).Length > 1 Then SB.Append(Word(i).SubString(1).ToLowerCase)
    Next
    Return SB.ToString
End Sub
Using this code snippet to trim extra spaces: https://www.b4x.com/android/forum/threads/b4x-greedy-trim.140808/
 
Top