In JS/Java, determine whether the string contains Chinese Characters

Source: Internet
Author: User

Similar code on the network is generally used by JS to determine whether all strings are Chinese, but few codes are used to determine whether the strings contain Chinese characters. Three methods are provided here:

 

<Script language = "JavaScript"> function funcchina () {var OBJ = document.form1.txt name. value; If (/. * [\ u4e00-\ u9fa5] +. * $ /. test (OBJ) {alert ("cannot contain Chinese characters! "); Return false;} return true ;} </SCRIPT> <form name = "form1"> <input type = "text" name = "txtname"> <input type = "button" name = "buttxt" value =" determine whether it is a Chinese character "onclick =" funcchina () "> </form>

 

 

<Script language = "JavaScript"> function ischina (s) {var patrn =/[\ u4e00-\ u9fa5] | [\ ufe30-\ uffa0]/GI; If (! Patrn.exe C (s) {return false;} else {return true;} alert (ischina ("www.zzsky.cn"); </SCRIPT>

 

 

<Script language = "JavaScript"> var STR = 'webmaster of China sky www.zzsky.cn '; If (escape (STR ). indexof ("% u") <0) {alert (" 文 ");} else {alert (" 文 ") ;}</SCRIPT>

In Java:

/*** Determine whether STR is a Chinese character. ** @ Param Str * @ return */Private Static Boolean chineseencode (string Str) {char [] chars = Str. tochararray (); Boolean isgb2312 = false; For (INT I = 0; I <chars. length; I ++) {byte [] bytes = ("" + chars [I]). getbytes (); If (bytes. length = 2) {int [] ints = new int [2]; ints [0] = bytes [0] & 0xff; ints [1] = bytes [1] & 0xff; if (ints [0]> = 0x81 & ints [0] <= 0xfe & ints [1]> = 0x40 & ints [1] <= 0xfe) {isgb2312 = true; break ;}} return isgb2312 ;}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.