Android Question First character in String to Uppercase

Declan

Well-Known Member
Licensed User
Longtime User
I have a number of strings that represent the names of books.
Example:
B4X:
answers_for_dummies_101
Using the StringFunctions Library, I split this string and create a List.
I then convert this List to a String
B4X:
        Dim BookNameList As List
        Dim BookNameFinal As String
        BookNameList = sf.Split(book, "_")
        BookNameFinal = sf.ListToString(BookNameList, False, False)
This works and I get:
B4X:
answers for dummies 101
How could I ensure that the first letter of each word is in Uppercase - only if it is a word and not a number.
I need to change:
B4X:
answers_for_dummies_101
To:
B4X:
Answers For Dummies 101
 

Star-Dust

Expert
Licensed User
Longtime User
B4X:
Dim BookNameList As List
Dim BookNameFinal As String =""
Dim S as String

BookNameList =Regex.Split("_", book)

For i=0 to BookNameList.Size-1
S=BookNameList.Get(i)
BookNameFinal = BookNameFinal  & S.Substring2(0,1).ToUppercase & S.SubString(1) & " "
Next
BookNameFinal=BookNameFinal.Trim
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You could use this: https://www.b4x.com/android/forum/threads/proper-case-for-names.41189/

Copy the code to a new class (called ProperCase) and and call it using:

B4X:
Dim PC As ProperCase
PC.Initialize

Log(PC.NameToProperCase("answers_for_dummies_101".Replace("_"," ")))

Might be a bit overkill for your requirement, but you can use it for a more if required.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
@SD
Many thanks - perfect

Alternative

B4X:
Dim BookNameList() As String = Regex.Split("_",book)
Dim BookNameFinal As String = ""

For Each Word As String In BookNameList
     BookNameFinal = BookNameFinal & Word.Substring2(0,1).ToUppercase & W.SubString(1) & " "
Next

BookNameFinal=BookNameFinal.Trim
 
Upvote 0
Top