To write code that works seamlessly for any language using any script, there are a few things to keep in mind.
| Consideration | Reason | 
|---|---|
| Avoid methods that use the chardata type. | Avoid using the charprimitive data type or methods that use thechardata type, because code that uses that data type does not work for supplementary characters. For methods that take achartype parameter, use the correspondingintmethod, where available. For example, use theCharacter.isDigit(int)method rather thanCharacter.isDigit(char)method. | 
| Use the isValidCodePointmethod to verify code point values. | A code point is defined as an intdata type, which allows for values outside of the valid range of code point values from 0x0000 to 0x10FFFF. For performance reasons, the methods that take a code point value as a parameter do not check the validity of the parameter, but you can use theisValidCodePointmethod to check the value. | 
| Use the codePointCountmethod to count characters. | The String.length()method returns the number of code units, or 16-bitcharvalues, in the string. If the string contains supplementary characters, the count can be misleading because it will not reflect the true number of code points. To get an accurate count of the number of characters (including supplementary characters), use thecodePointCountmethod. | 
| Use the String.toUpperCase(int codePoint)andString.toLowerCase(int codePoint)methods rather than theCharacter.toUpperCase(int codePoint)orCharacter.toLowerCase(int codePoint)methods. | While the Character.toUpperCase(int)andCharacter.toLowerCase(int)methods do work with code point values, there are some characters that cannot be converted on a one-to-one basis. The lowercase German character ß, for example, becomes two characters, SS, when converted to uppercase. Likewise, the small Greek Sigma character is different depending on the position in the string. TheCharacter.toUpperCase(int)andCharacter.toLowerCase(int)methods cannot handle these types of cases; however, theString.toUpperCaseandString.toLowerCasemethods handle these cases correctly. | 
| Be careful when deleting characters. | When invoking the StringBuilder.deleteCharAt(int index)orStringBuffer.deleteCharAt(int index)methods where the index points to a supplementary character, only the first half of that character (the firstcharvalue) is removed. First, invoke theCharacter.charCountmethod on the character to determine if one or twocharvalues must be removed. | 
| Be careful when reversing characters in a sequence. | When invoking the StringBuffer.reverse()orStringBuilder.reverse()methods on text that contains supplementary characters, the high and low surrogate pairs are reversed which results in incorrect and possibly invalid surrogate pairs. |