判断中文长度

public class ChinseUtil {
    public ChinseUtil() {
    }


    public static boolean isChineseStr(String str) {
        Pattern pattern = Pattern.compile("[一-龥·]");
        char[] c = str.toCharArray();


        for(int i = 0; i < c.length; ++i) {
            Matcher matcher = pattern.matcher(String.valueOf(c[i]));
            if (!matcher.matches()) {
                return false;
            }
        }


        return true;
    }


    public static int getLength(String s) {
        int valueLength = 0;
        String chinese = "[一-龥]";


        for(int i = 0; i < s.length(); ++i) {
            String temp = s.substring(i, i + 1);
            if (temp.matches(chinese)) {
                ++valueLength;
            } else {
                ++valueLength;
            }
        }


        return valueLength;
    }
}


示例:ChinseUtil.getLength(indexDetailDraftDto.getProductName()) > 18

猜你喜欢

转载自blog.csdn.net/qq_35781178/article/details/81064311