Skip to content

Instantly share code, notes, and snippets.

@KengoTODA
Created December 12, 2010 12:48
Show Gist options
  • Save KengoTODA/738013 to your computer and use it in GitHub Desktop.
Save KengoTODA/738013 to your computer and use it in GitHub Desktop.
/**
* Returns a new string that is a substring of this string.
* The surrogate pair is taken into consideration.
*/
final class SubstringSample {
static String substring(String source, int startCodePoints) {
final int endCodePoints = source.codePointCount(0, source.length());
return substring(source, startCodePoints, endCodePoints);
}
static String substring(String source, int startCodePoints, int endCodePoints) {
final int startIndex = source.offsetByCodePoints(0, startCodePoints);
final int endIndex = source.offsetByCodePoints(startIndex, endCodePoints - startCodePoints);
return source.substring(startIndex, endIndex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment