Java examples for java.lang:String Unicode
Gets the Unicode code point at the given index of the string.
/*// ww w . j a v a 2 s . c o m Written in 2013 by Peter O. Any copyright is dedicated to the Public Domain. http://creativecommons.org/publicdomain/zero/1.0/ If you like this, you should donate to Peter O. at: http://upokecenter.dreamhosters.com/articles/donate-now-2/ */ //package com.java2s; public class Main { /** * 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; } }