Page 1 of 2
Convert a string to title case
Posted: Sat Mar 29, 2025 3:46 pm
by Quin
I needed to convert a string to title case in my app, so I wrote this. Usage is incredibly simple, just pass it a string to convert, and it'll be returned to you with all the proper title-casing semantic rules applied (e.g. the, a, etc. are only capitalized if they're the first word). It's also explicit safe
Code: Select all
Macro AddExcludedWord(_Word_) : AddElement(ExcludedWords$()) : ExcludedWords$() = _Word_ : EndMacro
Procedure.b ListContainsString(List List$(), String$)
String$ = LCase(String$)
ForEach List$()
If LCase(List$()) = String$ : ProcedureReturn #True : EndIf
Next
ProcedureReturn #False
EndProcedure
; Converts a string to title case, respecting all the semantic rules that come along with that. For example, the at the very beginning of the string is capitalized, but anywhere else it isn't.
Procedure$ Capitalize(String$)
Protected.i i, Count
Protected Res$, Word$, FirstCharacter$, WordTail$
Static NewList ExcludedWords$()
AddExcludedWord("a")
AddExcludedWord("an")
AddExcludedWord("the")
AddExcludedWord("and")
AddExcludedWord("but")
AddExcludedWord("or")
AddExcludedWord("for")
AddExcludedWord("nor")
AddExcludedWord("on")
AddExcludedWord("at")
AddExcludedWord("to")
AddExcludedWord("by")
AddExcludedWord("of")
AddExcludedWord("in")
AddExcludedWord("up")
AddExcludedWord("with")
AddExcludedWord("as")
Count = CountString(String$, " ") + 1
For i = 1 To Count
Word$ = StringField(String$, I, " ")
FirstCharacter$ = Left(Word$, 1)
WordTail$ = Right(Word$, Len(Word$) - 1)
If i = 1 Or Not ListContainsString(ExcludedWords$(), Word$) : FirstCharacter$ = UCase(FirstCharacter$) : EndIf
Word$ = FirstCharacter$ + WordTail$
Res$ + Word$ + " "
Next
Res$ = RTrim(Res$, " ")
ProcedureReturn Res$
EndProcedure
Enjoy!
Re: Convert a string to title case
Posted: Sun Mar 30, 2025 2:45 am
by BarryG
Your code is missing the ListContainsString() procedure, so we can't test it (it's not compilable).
Re: Convert a string to title case
Posted: Sun Mar 30, 2025 4:50 am
by idle
there is also the UTF16 module that does Title case
https://github.com/idle-PB/UTF16/blob/main/UTF16.pb
Code: Select all
Procedure StrTCase_(*in.Unicode,sep=' ') ;changes the case of the string inplace
Protected *char.Unicode,tchar.u,bnext=1
*char = *in
While *char\u
If *Char\u = sep
bnext= 1
*char+2
Continue
ElseIf bnext
bnext = 0
tchar = casemappingTC(*char\u)
Else
tchar = casemappingLC(*char\u)
EndIf
If tchar
*char\u = tchar
EndIf
*char+2
Wend
EndProcedure
Re: Convert a string to title case
Posted: Sun Mar 30, 2025 5:48 am
by Randy Walker
There's also a freeware app to do any Case and it's called "AnyCase".
Do all Upper, All lower. Sentence case, Title Case. Toggle case. AlTeRnAtInG CaSe. And more. Works anywhere. It's great!
Don't pass it a string. Just highlight a string and click the desired button. It changes text in place.
Re: Convert a string to title case
Posted: Sun Mar 30, 2025 6:18 am
by idle
Randy Walker wrote: Sun Mar 30, 2025 5:48 am
There's also a freeware app to do any Case and it's called "AnyCase".
Do all Upper, All lower. Sentence case, Title Case. Toggle case. AlTeRnAtInG CaSe. And more. Works anywhere. It's great!
Don't pass it a string. Just highlight a string and click the desired button. It changes text in place.
That's not really a tip or trick.
Re: Convert a string to title case
Posted: Sun Mar 30, 2025 6:52 am
by Randy Walker
idle wrote: Sun Mar 30, 2025 6:18 am
Randy Walker wrote: Sun Mar 30, 2025 5:48 am
There's also a freeware app to do any Case and it's called "AnyCase".
Don't pass it a string. Just highlight a string and click the desired button. It changes text in place.
That's not really a tip or trick.
Sorry. Didn't even notice we were in Tips and Tricks.
Although, one might argue it might qualify as a tip.
Re: Convert a string to title case
Posted: Sun Mar 30, 2025 8:27 am
by BarryG
Randy Walker wrote: Sun Mar 30, 2025 6:52 amone might argue it might qualify as a tip
No, this area is for PureBasic tips and tricks, not third-party apps.
Re: Convert a string to title case
Posted: Sun Mar 30, 2025 8:40 am
by Randy Walker
BarryG wrote: Sun Mar 30, 2025 8:27 am
No, this area is for PureBasic tips and tricks, not third-party apps.
Fair point. Thanks for setting me straight. I hope I didn't spoil the mood. Still waiting to see the missing procedure.
Re: Convert a string to title case
Posted: Sun Mar 30, 2025 9:20 am
by Quin
Oops...it's been a long weekend. Procedure now added.
Re: Convert a string to title case
Posted: Sun Mar 30, 2025 10:03 pm
by Randy Walker
Thanks Quin. Works Great!
Re: Convert a string to title case
Posted: Mon Mar 31, 2025 4:48 am
by Seymour Clufley
As a single procedure:
Code: Select all
Procedure$ Capitalize(String$)
Protected.i i, Count
Protected Res$, Word$, FirstCharacter$, WordTail$
excl_arr.s = "|a|an|the|and|but|or|for|nor|on|at|to|by|of|in|up|with|as|"
Count = CountString(String$, " ") + 1
For i = 1 To Count
Word$ = StringField(String$, I, " ")
FirstCharacter$ = Left(Word$, 1)
WordTail$ = Right(Word$, Len(Word$) - 1)
If i = 1 Or Not FindString(excl_arr,"|"+Word$+"|") : FirstCharacter$ = UCase(FirstCharacter$) : EndIf
Word$ = FirstCharacter$ + WordTail$
Res$ + Word$ + " "
Next
Res$ = RTrim(Res$, " ")
ProcedureReturn Res$
EndProcedure
Re: Convert a string to title case
Posted: Mon Mar 31, 2025 5:47 am
by Randy Walker
Thanks Seymour Clufley. I like the string method better than the list method. Even if you did basically steal the code otherwise.

Personally happy to see anyone steal my code if they can make it better.
Re: Convert a string to title case
Posted: Mon Mar 31, 2025 12:54 pm
by Quin
Seymour Clufley wrote: Mon Mar 31, 2025 4:48 am
As a single procedure:
Code: Select all
Procedure$ Capitalize(String$)
Protected.i i, Count
Protected Res$, Word$, FirstCharacter$, WordTail$
excl_arr.s = "|a|an|the|and|but|or|for|nor|on|at|to|by|of|in|up|with|as|"
Count = CountString(String$, " ") + 1
For i = 1 To Count
Word$ = StringField(String$, I, " ")
FirstCharacter$ = Left(Word$, 1)
WordTail$ = Right(Word$, Len(Word$) - 1)
If i = 1 Or Not FindString(excl_arr,"|"+Word$+"|") : FirstCharacter$ = UCase(FirstCharacter$) : EndIf
Word$ = FirstCharacter$ + WordTail$
Res$ + Word$ + " "
Next
Res$ = RTrim(Res$, " ")
ProcedureReturn Res$
EndProcedure
Very nice! Only reason I didn't do it this way is because I don't like how slow StringField is, but a linked list is probably overkill, I could probably make it a static array.
Re: Convert a string to title case
Posted: Mon Mar 31, 2025 7:46 pm
by Randy Walker
Quin wrote: Mon Mar 31, 2025 12:54 pm
Very nice! Only reason I didn't do it this way is because I don't like how slow StringField is, but a linked list is probably overkill, I could probably make it a static array.
OK, you lost me. Both snippets use stringfield().

Re: Convert a string to title case
Posted: Tue Apr 01, 2025 7:06 am
by Seymour Clufley
Quin wrote: Mon Mar 31, 2025 12:54 pmVery nice! Only reason I didn't do it this way is because I don't like how slow StringField is, but a linked list is probably overkill, I could probably make it a static array.
My method uses FindString, not StringField.