Java中utf-8编码方式时所占字节数的计算方法封装

  1. /** 
  2.  * 计算采用utf-8编码方式时字符串所占字节数 
  3.  *  
  4.  * @param content 
  5.  * @return 
  6.  */  
  7. public static int getByteSize(String content) {  
  8.     int size = 0;  
  9.     if (null != content) {  
  10.         try {  
  11.             // 汉字采用utf-8编码时占3个字节  
  12.             size = content.getBytes("utf-8").length;  
  13.         } catch (UnsupportedEncodingException e) {  
  14.             e.printStackTrace();  
  15.         }  
  16.     }  
  17.     return size;  
  18. }  

你可能感兴趣的:(Java中utf-8编码方式时所占字节数的计算方法封装)