Example usage for java.lang String codePointCount

List of usage examples for java.lang String codePointCount

Introduction

In this page you can find the example usage for java.lang String codePointCount.

Prototype

public int codePointCount(int beginIndex, int endIndex) 

Source Link

Document

Returns the number of Unicode code points in the specified text range of this String .

Usage

From source file:org.omegat.tokenizer.BaseTokenizer.java

protected String[] tokenizeByCodePointToStrings(String strOrig) {
    // See http://www.ibm.com/developerworks/library/j-unicode/#1-5
    // Example 1-5 appears to be faster than 1-6 for us (because our strings are short?)
    String[] tokens = new String[strOrig.codePointCount(0, strOrig.length())];
    for (int cp, i = 0, j = 0; i < strOrig.length(); i += Character.charCount(cp)) {
        cp = strOrig.codePointAt(i);/*from   w  ww.ja  v a 2 s  .c o m*/
        tokens[j++] = String.valueOf(Character.toChars(cp));
    }
    return tokens;
}

From source file:org.zanata.magpie.util.CountUtil.java

/**
 * Count the characters in a string by code point.
 * See https://stackoverflow.com/a/6846155/345718
 *
 * @param content//from   ww  w.  j a v  a 2 s . c  o  m
 *            the string content
 * @return character count
 */
public static long countCharacters(@Nonnull String content) {
    if (StringUtils.isBlank(content)) {
        return 0;
    }
    return content.codePointCount(0, content.length());
}

From source file:password.pwm.util.java.StringUtil.java

public static int[] toCodePointArray(final String str) {
    if (str != null) {
        final int len = str.length();
        final int[] acp = new int[str.codePointCount(0, len)];

        for (int i = 0, j = 0; i < len; i = str.offsetByCodePoints(i, 1)) {
            acp[j++] = str.codePointAt(i);
        }/*  www .  ja  v a2s  . co  m*/

        return acp;
    }

    return new int[0];
}

From source file:password.pwm.util.StringUtil.java

public static int[] toCodePointArray(String str) {
    if (str != null) {
        int len = str.length();
        int[] acp = new int[str.codePointCount(0, len)];

        for (int i = 0, j = 0; i < len; i = str.offsetByCodePoints(i, 1)) {
            acp[j++] = str.codePointAt(i);
        }// www  . j  a va2  s  .  com

        return acp;
    }

    return new int[0];
}

From source file:txyd.util.StringUtils.java

/**
 * ?????/*from  www .  ja  v a 2 s  . c om*/
 * charjavachar16????16???????
 * <p>
 * ??U+0000U+10FFFFU+0000U+FFFFU+10000U+10FFFF
 * <p>
 * ????1216??UTF-16
 * ??????????U+D800U+DFFF
 * ???????
 *
 * @param sentence
 */
public static void printlnChar(String sentence) {
    //        String sentence = "\u03C0\uD835\uDD6B";//?
    //        String sentence = "";
    int lengthU = sentence.length();
    int lengthP = sentence.codePointCount(0, lengthU);
    //      System.out.println(lengthU);        // ??
    //      System.out.println(lengthP);        // ???
    if (lengthU != lengthP) {//
        for (int i = 0; i < lengthU; i++) {
            int codePoint = sentence.codePointAt(i);
            if (Character.isSupplementaryCodePoint(codePoint)) {
                System.out.println(String.valueOf(Character.toChars(codePoint)));
                i++;
            } else {
                System.out.println(sentence.charAt(i));
            }
        }
    }
}