Blog

Winter ’13 – Favourite features

2

The standard approach following any salesforce release is to write a blog post focusing on the latest and greatest functionality. This isn’t going to be one of those posts, rather than sticking with the “status quo” we’re going to buck the trend and write about one of the most boring (yet infinitely useful) new features introduced by Winter ’13 – new methods on the string object.

By making these methods available natively in Force.com, salesforce are continuing to open up the platform to the developer community at large and help them produce results faster than ever before. Gone are the days of having to write a regular expression to identify a string as numeric, or to check which case is being used.

We recognise that the Winter ’13 release contains numerous great new features, but ¬†it’s nice to focus on the simple improvements to the core functionality every once in a while!

New methods on String Object

The list below contains each new string method implemented with Salesforce Winter ’13:

  • abbreviate
  • center
  • join
  • repeat
  • rightPad
  • capitalize
  • swapCase
  • isAllLowerCase
  • containsAny
  • containsNone
  • countMatches
  • indexOfAny
  • substringAfter
  • substringBetween
  • escapeHTML3
  • excapeXml
  • unescapeXml
  • isAlpha
  • isNumeric
  • isEmpty
  • isNotEmpty

Let’s take a look at some of these methods in detail, to see how they can be used and the actual results returned.

swapCase

Swaps the case of all characters and returns the resulting String. Upper case and title case converts to lower case, and lower case converts to upper case. See the examples below for a better understanding of how this can be used:

String allupper = 'ALLUPPERCASE';
system.debug(allupper.swapCase());
Result: alluppercase

String mixcase = 'MiXcAsE';
system.debug(mixcase.swapCase());
Result: mIxCaSe

isNumeric

Returns boolean true if the current String contains only Unicode digits (numbers); otherwise returns false.

String numbers= 'ABCDEFG';
system.debug(numbers.isNumeric());
Result: false

String numbers = '12345';
system.debug(numbers.isNumeric());
Result: true

countMatches

By providing a value to the countMatches method, we can easily count how many times a specific letter, word or phrase is used within a given string.

String countmatches = 'ABCDEFGAAAA';
system.debug(countmatches.countMatches('A'));
Result: 5

repeat

Returns the current String repeated the specified number of times using the specified separator to separate the repeated Strings.

String repeatme = 'Broken record';
system.debug(repeatme.repeat('-', 2));
Result: Broken record-Broken record

isEmpty

Returns true if the list has zero elements (e.g. it’s empty!)

String empty = null;
system.debug(emtpy.isEmpty());
Result: true

String notempty = 'hello world';
system.debug(notempty.isEmpty());
Result: false

Whether you’re a Salesforce developer just getting started with Apex code or a seasoned professional, it’ll pay to familiarise yourself with these new methods as it’s likely that that’ll come in handy on a regular basis.

Other favourite features new in Winter ’13

  • New chart types for Visualforce
  • Inline editing support for Visualforce picklists <apex:inlineEditSupport>
  • Loading of test data from static resources
  • Support for Callout testing –¬†HttpCalloutMock interface

If you have any questions about this post, or any of the topics covered please get in touch!

  • I’m getting a syntax error with str.isEmpty() (Method does not exist) even though I’ve switched myClass.cls-meta.xml to apiVersion 26.0 Any ideas?

    • willcobbett

      Hi Marc, this looks like a bug – I just tried isEmpty/isNotEmpty and got the same result. The other methods appear to be working ok.