Java check whether the string contains spaces and Chinese
package com.demo.uninstaller.utils; import java.util.regex.Pattern; import org.apache.log4j.Logger; public class ChineseCharUtil { private static Logger log = Logger.getLogger(ChineseCharUtil.class); // true 时Check正常 public static boolean CheckChineseAndBlankCorrect(String checkString) { return (!hasChineseByRange(checkString)) && (checkString.indexOf(" ") == -1); } public static void main(String[] args) { log.debug(CheckChineseAndBlankCorrect("份额无法 3fes")); // log.debug (hasChineseByRange ( "share not 3fes")); } / ** * contains Chinese characters <br> * contains Chinese punctuation <br> * * @param str * @return * / public static Boolean hasChinese (String STR) { IF (STR == null ) { return to false ; } char [] = CH str.toCharArray (); for ( char C: CH) { IF (isChinese (C)) { return to true; } } Return false ; } / ** * Are all Chinese characters <br> * contains Chinese punctuation <br> * * @param str * @return * / public static boolean isChinese (String str) { IF (str = = null ) { return to false ; } char [] = CH str.toCharArray (); for ( char C: CH) { IF (! isChinese (C)) { return to false ; } } return to true ; } / ** * whether Chinese characters <br> * Chinese punctuation comprises <br> * * @param C * @return * / Private static Boolean isChinese ( char C) { Character.UnicodeBlock UB = The Character.UnicodeBlock.of (C); IF (UB == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) { return to true ; } the else IF (UB == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS) { return true; } else if (ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION) { return true; } else if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A) { return true; } else if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B) { return true; } else if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C) { return true; } else if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D) { return true; } else if (ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) { return true; } else if (ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) { return true; } return false; } / ** * contains characters <br> * The character code is judged <br> range * CJK Unified kanji (Chinese does not include the,. "" () " ' '" ,! ¥ symbols, etc.) <br> * * @param STR * @return * / public static Boolean hasChineseByReg (String STR) { IF (STR == null ) { return to false ; } the pattern pattern = of Pattern.compile ( "[u4E00 \\ - \\ u9FBF] +" ); return Pattern.matcher (STR) .find (); } / ** * whether all characters <br> * * The character code is judged <br> range * CJK Unified Chinese characters (not including the Chinese,. "" () " ' '" ,! ¥ symbol, etc.) <br> * STR@Param STR * @return * / public static Boolean isChineseByReg (String STR) { IF (STR == null ) { return to false ; } the Pattern pattern = of Pattern.compile ( "[u4E00 \\ - \\ u9FBF] +" ) ; return Pattern.matcher (STR) .matches (); } / ** * contains characters <br> * the character code is judged <br> range * CJK unified kanji (Chinese does not contain the "" (). " '' ",! ¥ symbol, etc.) <br> * * @param * @Return * / public static Boolean hasChineseByRange (String STR) { IF (STR == null ) { return to false ; } char [] = CH str.toCharArray (); for ( char C: CH) { IF (C> = && C 0x4E00 <= 0x9FBF ) { return to true ; } } return to false ; } / ** * whether all characters <br> * The character code is judged <br> range * CJK unified kanji (Chinese does not include the, "" (.) " ' '" ,! ¥ symbols, etc.) <br> * * @param str * @return * / public static boolean isChineseByRange (String str) { IF (str = = null ) { return to false ; } char [] = CH str.toCharArray (); for ( char C: CH) { IF (C <|| C 0x4E00> 0x9FBF ) { return to false ; } } return to true ; } }