Java 区分文本中的中英文字符函数


复制代码 代码如下:

public class EnCnTest
  {
  public static void main(String args[])
  {
  byte buf[] = args[0].getBytes();
  boolean halfChinese = false;
  for(int i=0;i
  {
  if(halfChinese)
  {
  byte ch[] = {buf[i-1],buf[i]};
  System.out.println("索引"+(i-1)+"位置为中文("+new String(ch)+")");
  halfChinese = false;
  continue;
  }
  if((buf[i] & 0x80) == 0)
  {
  System.out.println("索引"+i+"位置是英文("+(char)buf[i]+")");
  }
  else
  {
  halfChinese = true;
  }
  }
  }
  }


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3