Make Product Titles Shorter
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.
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]; ...
Get Amazon Hacks now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.