Title Case Ports

Background

These are ports of John Gruber's Title Case Perl script to Ruby, JavaScript, and Objective-C. All are released under the same license as the original code. If you make any improvements, please e-mail me — my user name on this domain is "marshall" — so that I can link to them (or host them if you need).

Title Case for Ruby

This is a direct port of the Perl code to Ruby. It reads lines from standard input and writes them to standard output. I deliberately tried to keep it as close to the original code as possible, but it could certainly be adapted to a String class extension.

Download titlecase.rb

Title Case for JavaScript

This adds a toTitleCase() method to all String objects that returns the title-cased version of the string. The title-case method can be used by itself as well, so you can use it with Prototype for iterating through collections of strings. For example:

    // As a String method:
    var title = line.toTitleCase();

    // Using the Prototype each() method:
    var titles = lines.each(TitleCase.toTitleCase);
  

Download titlecase.js

Title Case for Objective-C

This is an Objective-C category that adds a titlecaseString method to all NSString objects. It includes a small program that tests each of the edge cases.

The algorithm for this version is a wide departure from the original, due to the lack of regular expression support in Cocoa. NSPredicate can be used to test strings, but (as far as I can tell) it doesn't do replacement of grouped matches. There are third-party libraries that add regular expression matching to NSString, but I wanted to keep the requirements for this as low as possible — it might be useful in an iPhone app — and I figured it wouldn't hurt to better familiarize myself with NSScanner and NSCharacterSet.

Download VCTitleCase.zip