string 截取字符串包含汉字的情况

解决办法就是:将截取的最后一个字节与紧跟在后面的一个字节进行组合(考虑后面已经没字节的情况),判断组合出来的一个字符是否包含在原字符串中:如果包含,则说明是前半个汉字;如果不包含,则说明不是前半个汉字。(应该也无法区分汉字、韩文、日文)。
public static  String leftStr ( String source,  int  maxByteLen,  int  flag ){
         if ( source ==  null  || maxByteLen <=  0 ){
             return  "" ;
         }
         byte []  bStr = source.getBytes () ;
         if ( maxByteLen >= bStr.length ) return  source;
         String cStr =  new  String ( bStr, maxByteLen -  1 ,  2 ) ;
         if ( cStr.length ()  ==  1  && source.contains ( cStr )){
             maxByteLen += flag;
         }
         return new  String ( bStr,  0 , maxByteLen ) ;
     }

猜你喜欢

转载自fjg0427.iteye.com/blog/1635026