Let's say you wanted to create a String constant of a Unicode non-breaking whitespace. This is Unicode U+00A0

String nbsp = ????;

The SFDC doc is maddeningly incomplete on this topic. The section on Primitives doesn't say anything relevant and if your thoughts turned to escape sequences, what is listed doesn't address Unicode:

Escape Sequences: All Strings in Apex use the same escape sequences as SOQL strings: \b (backspace), \t (tab), \n (line feed), \f (form feed), \r (carriage return), \" (double quote), \' (single quote), and \ (backslash).

Does one escape Unicode sequences? If so, how?


SFDC doc tells you the answer in "indirect" places:

String class charAt method

String str = '\uD835\uDD0A';
System.assertEquals(55349, str.charAt(0), 
    'charAt(0) didn\'t return the high surrogate.');
System.assertEquals(120074, str.codePointAt(0),
    'codePointAt(0) didn\'t return the entire two-character supplementary value.');

also String class method codePointAtIndex

String str = '\u03A9 is Ω (Omega), and \uD835\uDD0A ' + 
    ' is Fraktur Capital G.';
System.assertEquals(937, str.codePointAt(0));
System.assertEquals(120074, str.codePointAt(20));
// Escaped or unescaped forms of the same character have the same code point
System.assertEquals(str.codePointAt(0), str.codePointAt(5));


String nbsp = '\u00A0';

Looking at Java doc (what Apex is based on), Unicode characters are not technically considered escape sequences, even though they start with \;

Instead, consider '\uxxxx' as a character (Unicode) literal and as legitimate as 'a' or '日'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.