Java String Index Of subStringChinese(String str, int startIndex, int endIndex)

Here you can find the source of subStringChinese(String str, int startIndex, int endIndex)

Description

sub String Chinese

License

Open Source License

Declaration

public static String subStringChinese(String str, int startIndex, int endIndex) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static String subStringChinese(String str, int startIndex, int endIndex) {
        int length = 0;
        int size = endIndex - startIndex;
        List<Character> charList = new ArrayList<Character>();
        for (char c : str.toCharArray()) {
            if (isChinese(c))
                length += 2;/*from w  w  w. ja  v a2  s . c  om*/
            else
                length++;
            charList.add(c);
            if (length >= size)
                break;
        }
        StringBuilder builder = new StringBuilder();
        for (Character c : charList) {
            builder.append(c);
        }
        return builder.toString();
    }

    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }
}

Related

  1. mergeStringLines(String lineOne, String lineTwo, int keyIndex, int insertingIndex)
  2. readColumn(int columnIndex, String inputString, String columnSeparator)
  3. removeColumn(String line, int index)
  4. splitUsingIndexOf(String splittee, String splitter)
  5. sub(String string, int fromIndex, int toIndex)