Upper case title - title case - capitalize

Share your advanced PureBasic knowledge/code with the community.
normeus
Enthusiast
Enthusiast
Posts: 415
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Upper case title - title case - capitalize

Post by normeus »

So I searched for all the wrong terms to Capitalize a sentence ( Yes, I didn't search for capitalize :shock: )
Since I was working on Javascript I wanted to find "Title Case".
Here is my code which I am just adding to the board so I can remember to always google search first.
And also a link to a whole bunch of other better samples of Capitalize:

http://www.purebasic.fr/english/viewtop ... 35#p370335

My shame code (if you use it, it also deletes spaces between words ):

Code: Select all

EnableExplicit
Procedure.s capitalize(string$)
  Protected n,i,phrase.s=LCase(string$),done.s = "Regx ERROR!",Dim wordsA.s(0)
  Protected rx=CreateRegularExpression(#PB_Any, "[a-z][^\s]*") 
  If rx >0
    n = ExtractRegularExpression(rx, phrase, wordsA())
    If n > 1
      done=""
      For i = 0 To n - 1
        done= done+UCase(Left(wordsA(i),1))+Mid(wordsA(i),2)+" "
      Next
      done=Left(done,Len(done)-1)
    Else
      done=UCase(Left(phrase,1))+Mid(phrase,2)
    EndIf
    FreeRegularExpression(rx) 
  EndIf
  ProcedureReturn done
EndProcedure



Debug capitalize("Take any CASE and capitalize each word.")
Debug capitalize("another phrase. To convert")
Debug capitalize("and here's another, one")
Debug capitalize("a")
Debug capitalize("SINGLE")
Again I am just posting it so that I remember to do a better search NEXT time :lol:

Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari