Java Unicode unicodePreservingIndex(String str, int index)

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

Description

Normalizes index such that it respects Unicode character boundaries in str .

License

Open Source License

Parameter

Parameter Description
str the String
index the index to be normalized

Return

a normalized index that does not split a Unicode character

Declaration

private static int unicodePreservingIndex(String str, int index) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    /**/* w  ww. jav  a 2  s  . c  o  m*/
     * Normalizes {@code index} such that it respects Unicode character
     * boundaries in {@code str}.
     *
     * <p>If {@code index} is the low surrogate of a unicode character,
     * the method returns {@code index - 1}. Otherwise, {@code index} is
     * returned.
     *
     * <p>In the case in which {@code index} falls in an invalid surrogate pair
     * (e.g. consecutive low surrogates, consecutive high surrogates), or if
     * if it is not a valid index into {@code str}, the original value of
     * {@code index} is returned.
     *
     * @param str the String
     * @param index the index to be normalized
     * @return a normalized index that does not split a Unicode character
     */
    private static int unicodePreservingIndex(String str, int index) {
        if (index > 0 && index < str.length()) {
            if (Character.isHighSurrogate(str.charAt(index - 1)) && Character.isLowSurrogate(str.charAt(index))) {
                return index - 1;
            }
        }
        return index;
    }
}

Related

  1. unicodeCodepointToString(char cp, boolean shortenIfPossible, String prefix, boolean upperCase)
  2. unicodeConvert(String str)
  3. unicodeCount(String sStr)
  4. unicodeEncode(String s)
  5. unicodeHTMLEscape(final String s)
  6. unicodePreservingSubstring(String str, int begin, int end)
  7. unicodeToChar(char[] unicode)
  8. unicodeToHTMLUnicodeEntity(final String text)
  9. unicodeTrim(String s)