在java中怎样判断全角半角

Python018

在java中怎样判断全角半角,第1张

JS判断全角半角

function chkHalf(str){

for(var i=0i<str.lengthi++)

{

strCode=str.charCodeAt(i)

if((strCode>65248)||(strCode==12288)){

alert("有全角字符")

break

}

}

}

JAVA判断全角半角

if(str.getBytes().length==string.length){

全是半角

}

if(str.getBytes().length >str.length &&str.getBytes().length != str.length){

混合

}

if(str.getBytes().length == str.length *2){

全是全角

}

----------------------------------------------------------------------------

可以直接用正则。

String input = "是否"

System.out.println(input.matches("[\u4E00-\u9FA5]+"))

匹配中文字符的正则表达式: [\u4e00-\u9fa5]

匹配双字节字符(包括汉字在内):[^\x00-\xff]

public class CHar {

public static void main(String[] args){

int i

String mystr="你好,hello"

System.out.println(mystr)

for(i=0i<mystr.length()i++){

char ch

ch=mystr.charAt(i)  //单个地获取每个字符

if ((int)ch>255) //如果字符转换为整数大于255说明是全角字符

System.out.print(ch+":全  ")

else

System.out.print(ch+":半  ")

}

}

}