package com.urt.module.util;
import java.io.UnsupportedEncodingException;
/**
*
* @author happyqing
* 2013.11.6
*/
public class StringUtil {
/**
*
* @param str
* @return
*/
public static int getLength(String str){
try {
return str.getBytes("UTF-8").length; // 3 。
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
/**
*
* @param str
* @return
*/
public static int getStrLength(String str){
if(str==null || str.length()<0){
return 0;
}
int len=0;
char c;
for(int i=str.length()-1;i>=0;i--){
c=str.charAt(i);
if (c > 255) {
/**//*
* GBK
* UTF-8 len += 3;
*/
len += 3;
} else {
len++;
}
}
return len;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
System.out.println(" a".getBytes("UTF-8").length); //7
System.out.println(StringUtil.getStrLength(" a")); //7
System.out.println(" a".length()); //3
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}