WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

String Manipulation

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Uppercase first character of each word

Keywords:   mixed case Uppercase first character of each word Propercase

Question:

Does anyone have a way to convert strings into a propercase string? For example: "tHis sTRing is Jacked" into "This String Is Jacked"

Answer:

Umm. What is the definition of ProperCase being used here.

It it is take string. Uppercase first character of each word. Lowercase remainder of word... then maybe...




;This Udf Converts A String From Random Mixed Case To "propercase" Where
; Only The First Words Of Strings Are Uppercase And The Rest Are Lowercase

;More advanced versions are loating around that understand quoted words,
;some words that do not get capitalized like the or of in strings,
;roman numerals, and common abbreviations. However all of those are
;very usage and language dependent.

#DefineFunction Propercase(sbad)
sbad=StrLower(sbad)
count=ItemCount(sbad," ")
sgood=""

For xx=1 To count
thisword=ItemExtract(xx,sbad," ")
thislen=StrLen(thisword)
If thislen > 1
thisword=StrCat(StrUpper(StrSub(thisword,1,1)),StrSub(thisword,2,-1))
Else
If thislen==1
thisword=StrUpper(thisword)
EndIf
EndIf
If sgood=="" Then sgood=thisword
Else sgood=StrCat(sgood," ",thisword)
Next
Return(sgood)

#EndFunction



TestString="tHis sTRing is Jacked"
FixedString=ProperCase(TestString)
Message(teststring,FixedString)


Here is a slightly smarter version that can handle normal capitalization exceptions - like some words are not capitalized, and some ancrnyms are fully capitalized.

CapLine="capitalize this like of characters the right way"
origline=capline
   
CapLine=StrTrim(StrLower(CapLine))
CapCount=ItemCount(CapLine," ")
NewCapLine=""
For capxx=1 To CapCount
 cw=ItemExtract(capxx,CapLine," ")
 If capxx==1 || ( capxx!=1 && cw!='a' && cw!="an" && cw!='to' && cw!='by' && cw!='the' && cw!='for' && cw!='and' && cw!='in' && cw!='about' && cw!='on' && cw!="of" && cw!="from" && cw!="with" && cw!="my" )
    If StrLen(cw) >= 2
        charone=StrUpper(StrSub(cw,1,1))
        If charone=="'" || charone=='"' || charone=="`"
           cw=StrCat ( StrUpper(StrSub(cw,1,2)) , StrSub(cw,3,-1))
        Else
           cw=StrCat ( charone , StrSub(cw,2,-1))
        EndIf
    Else
       If StrLen(cw==1) Then cw=StrUpper(cw)
    EndIf
 EndIf
 cw2=StrReplace(cw,".","")
 cw2=StrReplace(cw2,",","")
 If cw2=="Bc" || cw2=="Ad" || cw2=="Ii" || cw2=="Iii" || cw2=="Iv" || cw2=="Vi" || cw2=="Vii" || cw2=="Viii" || cw2=="Ix" Then cw=StrUpper(cw)
 Newcapline=StrCat(newcapline,cw," ")
Next
CapLine=StrTrim(newcapline)

Message(origline,capline)

Article ID:   W15765
File Created: 2005:02:01:10:55:18
Last Updated: 2005:02:01:10:55:18