Title Case

By Herc08 on Sep 06, 2014

This is just something that I came up with because I was bored. It is possible in MS Word, so I decided to match it. I haven't submitted anything in years. The first code is tried to be done without regex, but it is impossible (or just too lengthy) to add exceptions. It will capitalize the first letter of each word, besides: a, an, is, of, and, & the.

Yes, this is a simple script, just wanted to spread some code around. Both lines will go in the remote section.

;code by me
alias tcase { 
var %n = $gettok($1-,0,32), %i = 2, %t = $1- 
while (%i <= %n) { 
var %o = %o $+ $iif($regex($gettok(%t,%i,32),/^(an?|of|the|and|is|for)$/),$regml(1),$upper($left($gettok(%t,%i,32),1)) $+ $right($gettok(%t,%i,32),-1)) $+ $chr(27) 
 inc %i 
 } 
echo -a $upper($left($gettok(%t,1,32),1)) $+ $right($gettok(%t,1,32),-1) $+ $chr(32) $+ $replace(%o,$chr(27),$chr(32)) 
}

;coded by Membear
alias tcase {
  var %a = $regsubex($1-,/(?<=\s|^)([a-z])/g,$upper(\t))
  var %b = $regsubex(%a,/((?<=\s)(?:A|An|And|Of|The)(?=[^a-zA-Z]|$))/g,$lower(\t))
  return %b
}

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.