源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-03-17 10:22 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:从字符串中截取等长字节的Java代码
在页面显示的时候,有时候文字无法显示完全,就只能显示部分文字,但是直接截取就只能截取等长字符串,英文和中文很难处理 所以就写了下面方法,截取等长字符
[u]复制代码[/u] 代码如下:
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 ) + ".." ;   }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部