Guest User

Untitled

a guest
Jul 17th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. /**
  2. * Returns a new string that is a substring of this string.
  3. * The surrogate pair is taken into consideration.
  4. */
  5. final class SubstringSample {
  6. static String substring(String source, int startCodePoints) {
  7. final int endCodePoints = source.codePointCount(0, source.length());
  8. return substring(source, startCodePoints, endCodePoints);
  9. }
  10.  
  11. static String substring(String source, int startCodePoints, int endCodePoints) {
  12. final int startIndex = source.offsetByCodePoints(0, startCodePoints);
  13. final int endIndex = source.offsetByCodePoints(startIndex, endCodePoints - startCodePoints);
  14. return source.substring(startIndex, endIndex);
  15. }
  16. }
Add Comment
Please, Sign In to add comment