Hack 91 Make Product Titles Shorter

figs/moderate.giffigs/hack91.gif

There are tools for cutting and slicing strings in every programming environment. Here are some quick examples of cutting book titles down to size.

The ProductName that Amazon returns is always the full name of the product. It's good to be accurate, but sometimes you need only part of the title. For example, O'Reilly has a book called Malicious Mobile Code: Virus Protection for Windows (O'Reilly Computer Security), and this is exactly what Amazon sends as the ProductName value. Weighing in at 80 characters, it's a bit long when Malicious Mobile Code could work just as well.

91.1 The Code

By looking at book titles as a template, code can automate shortening the titles. In many cases they follow this pattern:

[short title]:[sub title]([series title])

Using string-dicing functions built into most languages for trimming is easy work.

JavaScript
var title = "Malicious Mobile Code: Virus Protection for Windows";
shortTitle = title.split(":")
alert(shortTitle[0]);
Perl
my $title = "Malicious Mobile Code: Virus Protection for Windows";
@shortTitle = split /:/, $title;
print $shortTitle[0];
VBScript
strTitle = "Malicious Mobile Code: Virus Protection for Windows"
shortTitle = Split(strTitle,":")
Wscript.Echo shortTitle(0)
PHP
$title = "Malicious Mobile Code: Virus Protection for Windows";
$shortTitle = split(":",$title);
echo $shortTitle[0];
Python
import string
title = "Malicious Mobile Code: Virus Protection for Windows";
shortTitle = string.split(title,":");
print shortTitle[0];

Each of these bits of code splits the string at the colon (:) and prints everything before it (i.e., Malicious Mobile Code). These code snippets would work with books that don't have colons as well by returning the entire title.