Created
December 12, 2010 12:48
-
-
Save KengoTODA/738013 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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