Convert a string to title case

Share your advanced PureBasic knowledge/code with the community.
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Convert a string to title case

Post 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!
Last edited by Quin on Sun Mar 30, 2025 9:20 am, edited 1 time in total.
BarryG
Addict
Addict
Posts: 4155
Joined: Thu Apr 18, 2019 8:17 am

Re: Convert a string to title case

Post by BarryG »

Your code is missing the ListContainsString() procedure, so we can't test it (it's not compilable).
User avatar
idle
Always Here
Always Here
Posts: 5855
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Convert a string to title case

Post 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   

Randy Walker
Addict
Addict
Posts: 1008
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Convert a string to title case

Post 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.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
idle
Always Here
Always Here
Posts: 5855
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Convert a string to title case

Post 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.
Randy Walker
Addict
Addict
Posts: 1008
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Convert a string to title case

Post 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.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
BarryG
Addict
Addict
Posts: 4155
Joined: Thu Apr 18, 2019 8:17 am

Re: Convert a string to title case

Post 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.
Randy Walker
Addict
Addict
Posts: 1008
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Convert a string to title case

Post 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.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Convert a string to title case

Post by Quin »

Oops...it's been a long weekend. Procedure now added.
Randy Walker
Addict
Addict
Posts: 1008
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Convert a string to title case

Post by Randy Walker »

Thanks Quin. Works Great!
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Seymour Clufley
Addict
Addict
Posts: 1265
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: Convert a string to title case

Post 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
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Randy Walker
Addict
Addict
Posts: 1008
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Convert a string to title case

Post 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. :mrgreen:
Personally happy to see anyone steal my code if they can make it better.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Convert a string to title case

Post 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.
Randy Walker
Addict
Addict
Posts: 1008
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Convert a string to title case

Post 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(). :shock:
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Seymour Clufley
Addict
Addict
Posts: 1265
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: Convert a string to title case

Post 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.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Post Reply