Last month, in the Winter ’13 Release Preview webinar, Dave and Sam mentioned that the Apex String class gains a number of new methods in this release; 68 to be exact. Every Apex developer should study the new methods in detail – using them will improve performance, make your code more readable and consume fewer script statements. Some of the new methods in particular carry such savings that you might even want to go back and refactor existing code to use them. In this blog entry, I’ll highlight one method from each of the groups listed in the Winter ’13 release notes. Download the release notes, open the Apex String documentation page and read on…

Manipulating Strings

First, let’s look at join, a static method that joins the elements of any iterable object, such as a List, into a String separated by a given separator. Here’s the example from the Apex docs

As you can see, in a single method call we can join an arbitrary number of elements into a single string – for even the simple example shown, using a for loop consumes 9 script statements instead of the 3 above; as the list size increases the savings multiply, exponentially if we are processing records in a loop.

Changing/checking case

In this set of methods, you might not need swapCase often, but you’ll be glad of it when you do. As its name implies, it simply swaps the case of all characters in a string:

Without swapCase, there is no way to achieve this result without scanning through the string character by character, consuming a number of script statements proportional to the string length. With swapCase, of course, you can swap the case of a string of any length in a single script statement.

Searching/extracting substrings

Here, countMatches achieves a simple goal, counting the number of times the specified substring occurs in the current String, but think of the code you would have to write in its absence – something like:

Here, although we’re only burning through three script statements per match, this would quickly become significant if you were, for example, counting occurrences of a common word in a sizable piece of text.

Escaping/unescaping Strings

Nothing really stands out here – there are methods to escape and unescape CSV, ‘EcmaScript‘ (aka JavaScript!), HTML3, HTML4 and XML, each based on its counterpart in the Apache Commons Lang StringEscapeUtils library. It almost goes without saying that you would consume several script statements per character of input implementing any of these ‘by hand’.

Determining the type of characters in a String

A family of closely related methods, here’s the usage for isAlphanumeric:

Pre-Winter ’13, you would have to roll your own test for alphanumericity (is that even a word?) and scan through the string character-by-character. Not fun.

Other utility methods

My eye is immediately drawn to getLevenshteinDistanceWikipedia says, ‘the Levenshtein distance is a string metric for measuring the difference between two sequences. Informally, the Levenshtein distance between two words is equal to the number of single-character edits required to change one word into the other’. One application of Levenshtein distance is to figure out if one word is likely a typo of another:

So, there you have it, 68 shiny new toys for every Apex developer. Which is your favorite? Leave a comment and let us know!

Photo credit: http://www.flickr.com/photos/slgc/6593874739/

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS