เมื่อทำงานในโครงการคุณมักจะพบปัญหาบางอย่างที่คุณต้องตัดสินว่าตัวละครเป็นภาษาจีนหรือไม่ดังนั้นคุณจึงได้รวบรวมตัวอย่างรหัสสำหรับการตัดสินตัวละครจีน นี่คือการแบ่งปันสำหรับการอ้างอิงของคุณ
ฉันโพสต์รหัสโดยตรงพร้อมความคิดเห็นโดยละเอียด
แพ็คเกจ com.coder4j.main; นำเข้า java.util.regex.pattern;/*** java ตัดสินตัวละครจีน** @author chinaxiang* @date 2015-08-11 **/ชั้นเรียนสาธารณะ checkchinese "สวัสดีทอม.!@#$%^&*() _+-= {} | []; ':/"? "; // จีนบริสุทธิ์ (ไม่รวมเครื่องหมายวรรคตอนภาษาจีน) สตริง s2 =" สวัสดีจีน "." ":"' '' () [] []! "╃"; สตริง S8 = "╂"; // สตริงจีนดั้งเดิม S9 = "เก่า"; // 1 ใช้ช่วงอักขระเพื่อตรวจสอบว่า system.out.println ("S1 มีภาษาจีน:" + มีภาษาจีน ภาษาจีน: " + มี chinesybyRange (S3)); // truesystem.out.println (" S4 มีภาษาจีน: " + มี chinesyByRange (S4)); // falsesystem.out.println (" S5 มีภาษาจีน: " ChineseByRange (S6)); // falsesystem.out.println ("S7 มีภาษาจีน:" + มี chinesyByRange (S7)); // falsesystem.out.println ("S8 มีภาษาจีน:" มีภาษาจีน) truesystem.out.println ("-------------------"); System.out.println ("S1 เป็นภาษาจีนทั้งหมด:" + เป็นภาษาจีน (s1)); // falsesystem.out.println ("S2 เป็นภาษาจีนทั้งหมด:" + เป็นภาษาจีน System.out.println ("S4 เป็นภาษาจีนทั้งหมด:" + ischinesybyRange (S4)); // falsesystem.out.println ("S5 เป็นภาษาจีนทั้งหมด:" + ischinesebyrange (S5)); Falsesystem.out.println ("S7 เป็นภาษาจีนทั้งหมด:" + ischinesebyRange (S7)); // falsesystem.out.println ("S8 เป็นภาษาจีนทั้งหมด:" + ischinesebyRange (S8)); truesystem.out.println ("---------------"); // 2 ใช้ช่วงตัวละครการตัดสินปกติ (ผลลัพธ์เหมือนกับ 1) System.out.println ("S1 ภาษาจีน:" + มีภาษาจีน truesystem.out.println ("S3 มีภาษาจีน:" + มีภาษาจีน (s3)); // truesystem.out.println ("S4 มีภาษาจีน:" + มีภาษาจีน (S4)); ภาษาจีน: " + มีภาษาจีน (s6)); // falsesystem.out.println (" S7 มีภาษาจีน: " + มีภาษาจีน (s7)); // falsesystem.out.println (" S8 มีภาษาจีน truesystem.out.println ("-------------------"); System.out.println ("S1 เป็นภาษาจีนทั้งหมด:" + เป็นภาษาจีน (s1)); // falsesystem.out.println ("s2 เป็นภาษาจีนทั้งหมด:" + is is is is is (s2)); // truesystem.out.println ("s3 เป็นภาษาจีนทั้งหมด System.out.println ("S4 เป็นภาษาจีนทั้งหมด:" + ischinesebyreg (s4)); // falsesystem.out.println ("S5 เป็นภาษาจีนทั้งหมด:" + ischinesebyreg (s5)); Falsesystem.out.println ("S7 เป็นภาษาจีนทั้งหมด:" + ischinesebyreg (s7)); // falsesystem.out.println ("S8 เป็นภาษาจีนทั้งหมด:" + ischinesebyreg (s8)); // falsesystem.out.println (" truesystem.out.println ("----------------"); // 3 ใช้ตัวอักษร CJK ที่ตั้งค่าเพื่อตรวจสอบว่า system.out.println ("S1 มีภาษาจีน:" + haschinese (s1)); // falsesystem.out.println ("s2 มีภาษาจีน " + มีภาษาจีน (s3)); // truesystem.out.println (" S4 มีภาษาจีน: " + มีภาษาจีน (S4)); // falsesystem.out.println (" S5 มีภาษาจีน: "มีภาษาจีน (S5)); // Falsesystem.out.println ("S7 มีภาษาจีน:" + มีภาษาจีน (s7)); // falsesystem.out.println ("S8 มีภาษาจีน:" + haschinese (s8)); // falsesystem.out.out.wintln ("S9 มีภาษาจีน truesystem.out.println ("-------------------"); System.out.println ("S1 เป็นภาษาจีนทั้งหมด:" + ischinese (S1)); // falsesystem.out.println ("S2 ทั้งหมดเป็นภาษาจีน:" ischinese (S2)); ischinese (S3)); // เครื่องหมายวรรคตอนจีนที่แท้จริงรวมอยู่ใน System.out.println ("S4 เป็นภาษาจีนทั้งหมด:" + ischinese (S4)); // falsesystem.out.println ("S5 เป็นภาษาจีนทั้งหมด:" ischinese (s5)); Falsesystem.out.println ("S7 เป็นภาษาจีนทั้งหมด:" + ischinese (S7)); // falsesystem.out.println ("S8 เป็นภาษาจีนทั้งหมด:" + ischinese (S8)); // falsesystem มีเครื่องหมายวรรคตอนจีน <br>** @param str* @return*/บูลีนคงที่สาธารณะมีภาษาจีน (str str) {ถ้า (str == null) {return false;} char [] ch = str.tochararray (); สำหรับ (char c: ch) {ถ้า ischinese (c)) @Param str* @return*/public Static Boolean ischinese (String str) {ถ้า (str == null) {return false;} char [] ch = str.toChararray (); สำหรับ (char c: ch) {ถ้า (! ischinese (c)) {return false;}}}}} @return*/private static boolean ischinese (char c) {ตัวละคร unicodeBlock ub = character.unicodeBlock.of (c); ถ้า (ub == character.unicodeblock.cjk_unified_ideographs) {return true; ตัวละคร unicodeBlock.cjk_symbols_and_punctuation) {return true;}} ถ้า (ub == character.unicodeblock.cjk_unified_ideographs_extension_a) {return true; ตัวละคร unicodeBlock.cjk_unified_ideographs_extension_c) {return true;} else ถ้า (ub == character.unicodeblock.cjk_unified_ideographs_extension_d) {ส่งคืนจริง; ตัวละคร unicodeBlock.halfwidth_and_fullwidth_forms) {return true;} return false;}/*** ไม่ว่าจะรวมตัวละครจีนหรือไม่ <br>* ผู้พิพากษาตามช่วงการเข้ารหัสของตัวละครจีน <br>* cjk unified chines @ @ไม่รวมภาษาจีน 《》 () '' '' ' str) {if (str == null) {return false;} pattern = pattern.compile ("[// u4e00-// u9fbf]+"); รูปแบบการคืนข้อมูล. matcher (str) .find ();}/*** ! ¥ และสัญลักษณ์อื่น ๆ ) <br>** @param str* @return*/public public boolean ischinesebyreg (string str) {ถ้า (str == null) {return false; รูปแบบรูปแบบ = รูปแบบ. compile ("[// u4e00-// u9fbf]+"); ในช่วงการเข้ารหัสของอักขระจีน <br>* ตัวละครจีน Unified unified (ไม่รวมภาษาจีน, "《》 ()" '' ',! ¥ และสัญลักษณ์อื่น ๆ ) <br>** @param str* @return*/บูลีนคงที่มี chinesbyrange (string) {ถ้า (str == null) 0x4e00 && c <= 0x9fbf) {return true;}} return false;}/*** ไม่ว่าตัวละครจีนทั้งหมดจะเป็นตัวละครจีนหรือไม่ <br>* ผู้ตัดสินตามช่วงการเข้ารหัสของตัวละครจีน <br>* cjk unified strass* @ @ @ @ @ @ @ @ @ @parparam strass {ถ้า (str == null) {return false;} char [] ch = str.toChararray (); สำหรับ (char c: ch) {ถ้า (c <0x4e00 || c> 0x9fbf) {return false;}} return true;}}}}}หากคุณตัดสินว่าเป็นภาษาจีนโดยไม่ต้องตัดสินเครื่องหมายวรรคตอนจีนขอแนะนำให้ใช้การจับคู่ปกติซึ่งอาจมีประสิทธิภาพมากกว่า
เนื้อหารหัสด้านบนแนะนำรหัสตัวอย่างของ Java เพื่อตัดสินตัวละครเป็นภาษาจีน (มีประโยชน์มาก) และฉันหวังว่ามันจะเป็นประโยชน์กับคุณ