`
Listen_ing
  • 浏览: 35345 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

StringTool(字符串工具类)

 
阅读更多
public class StringTool {


    /**
     * 获得用户请求的来源相对地
     * @param header(请求的全路径) 
     * @param contextPath(工程目录)
     * @return String
     */
    public static String formatURL(String header, String contextPath) {
       return header.substring(header.indexOf(contextPath)
              + contextPath.length());
    }
   
    /**
     * 获得字符串的长度,中文为两个字符长度
     * @param value
     * @return
     */
    public static int lengthOfQuanJiao(String value) {
       if (value == null)
           return 0;
       StringBuffer buff = new StringBuffer(value);
       int length = 0;
       String stmp;
       for (int i = 0; i < buff.length(); i++) {
           stmp = buff.substring(i, i + 1);


           try {
              stmp = new String(stmp.getBytes("utf8"));
           } catch (Exception e) {


           }


           if (stmp.getBytes().length > 1) {
              length += 2;
           } else {
              length += 1;
           }
       }
       return length;
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics