Here you can find the source of CodePointAt(String str, int index)
Parameter | Description |
---|---|
str | A string. |
index | Index of the current position into the string. |
Parameter | Description |
---|---|
NullPointerException | The parameter str is null. |
public static int CodePointAt(String str, int index)
//package com.java2s; public class Main { /**/*from w w w . ja v a2s . c o m*/ * Gets the Unicode code point at the given index of the string. * @param str A string. * @param index Index of the current position into the string. * @return The Unicode code point at the given position. Returns -1 if {@code * index} is less than 0, or is the string's length or greater. Returns * the replacement character (U + FFFD) if the current character is an * unpaired surrogate code point. * @throws NullPointerException The parameter {@code str} is null. */ public static int CodePointAt(String str, int index) { return CodePointAt(str, index, 0); } /** * Gets the Unicode code point at the given index of the string. * @param str A string. * @param index Index of the current position into the string. * @param surrogateBehavior Specifies what kind of value to return if the * previous character is an unpaired surrogate code point: if 0, return * the replacement character (U + FFFD); if 1, return the value of the * surrogate code point; if neither 0 nor 1, return -1. * @return The Unicode code point at the current position. Returns -1 if {@code * index} is less than 0, or is the string's length or greater. Returns * a value as specified under {@code surrogateBehavior} if the previous * character is an unpaired surrogate code point. * @throws NullPointerException The parameter {@code str} is null. */ public static int CodePointAt(String str, int index, int surrogateBehavior) { if (str == null) { throw new NullPointerException("str"); } if (index >= str.length()) { return -1; } if (index < 0) { return -1; } int c = str.charAt(index); if ((c & 0xfc00) == 0xd800 && index + 1 < str.length() && str.charAt(index + 1) >= 0xdc00 && str.charAt(index + 1) <= 0xdfff) { // Get the Unicode code point for the surrogate pair c = 0x10000 + ((c - 0xd800) << 10) + (str.charAt(index + 1) - 0xdc00); ++index; } else if ((c & 0xf800) == 0xd800) { // unpaired surrogate return (surrogateBehavior == 0) ? 0xfffd : ((surrogateBehavior == 1) ? c : (-1)); } return c; } }