网站首页 新闻首页 网页设计图形动画软件编程网站开发办公软件操作系统数据库网络技术认证考试范文资料黑客攻防 书籍教程 进入论坛

java全角空格转换半角空格

http://www.diybl.com/ 2007-12-6  网络 点击:  [ 评论 ]
文章搜索:    【点击打包该文章】

这里主要区别就在于,全角字符串的getBytes()的字解码和半角字符串的getBytes()字节码不一样。

半角的 字节码是 32,全角的字节码是 -95-95 ,我们只需在这里给它都替换成 32 就可以了。

我在这里提供了一个类方法,可以将全角空字节码 换成 32半角字节码。很有用哦。

public class RemoveSpaces {
 public static final String removeSpace(String ss){
     byte[] t = ss.getBytes();
     for(int i=0;i<t.length;i++){
       if(t[i] == -95 && t[i+1]==-95 ){
        t[i]=32;
        if(i+1==t.length-1){
           t[i+1] = 0;
        }else{
         for(int j=i+1;j+1<t.length;j++){
          t[j]=t[j+1];
          if(j+1 == t.length-1) t[j+1] = 32;
         }
        }
        
          }             
      }
     return new String(t);
    }

public static void main(String arg[]){

  String test = "我 是 好 人";

 String result=removeSpace(test );

   System.out.println(test +"\n"+result);

}
}

 



文章整理:DIY部落 http://www.diybl.com (本站)   【点击打包该文章】
如果图片或页面不能正常显示请点击这里 站内搜索:   
上一篇文章:JXTA HelloWorld攻略
下一篇文章:NetBeans 6.0 Release

文章评论

请您留言