当前位置: 移动技术网 > IT编程>开发语言>Java > 从字符串中截取等长字节的Java代码

从字符串中截取等长字节的Java代码

2019年07月22日  | 移动技术网IT编程  | 我要评论

长寿的人这个地方摸着非常软,新加坡狼群网,新金梅瓶下载

在页面显示的时候,有时候文字无法显示完全,就只能显示部分文字,但是直接截取就只能截取等长字符串,英文和中文很难处理
所以就写了下面方法,截取等长字符
复制代码 代码如下:

public static void main(string[] args) {

  string str = "20120131:《回家》1你好么" ;

  system.out.println( substring(str , 10 ) ) ;
 }
 public static string substring(string str , int len){
  len *= 2 ;
  byte[]bytes = str.getbytes() ;
  if(bytes.length <= len){
   return str ;
  }

  byte[]newbytes = arrays.copyof( bytes, len ) ;
  int count = 0 ;
  for(byte b : newbytes){
   if(b < 0){
    count++;
   }
  }
  if(count % 2 != 0){

   len ++;
   newbytes = arrays.copyof( bytes, len ) ;
  }

 
  return new string( newbytes ) + ".." ; 
 }

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网