Java Char Code Get CodePointAt(String str, int index)

Here you can find the source of CodePointAt(String str, int index)

Description

Gets the Unicode code point at the given index of the string.

License

Creative Commons License

Parameter

Parameter Description
str A string.
index Index of the current position into the string.

Exception

Parameter Description
NullPointerException The parameter str is null.

Return

The Unicode code point at the given position. Returns -1 if 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.

Declaration

public static int CodePointAt(String str, int index) 

Method Source Code

//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;
    }
}

Related

  1. codePointAt(char[] s, int i)
  2. codePointAt(final char c1, final char c2)
  3. codePointAt(String _this, int index)
  4. CodePointAt(String str, int index, int endIndex)
  5. codePointAt(String theString, int point)