Java 字符串截取, 中文英文截取相同长途

来源:互联网 发布:网页朗读软件 编辑:程序博客网 时间:2024/04/30 03:21
如果用substring() 来截取的话,中文和英文所占字符长度 不一样,如果在页面上显示的话,显示出来的效果,不是我们想要的


解决方法1

/**     * Calculation of the string length <font color="red">in bytes</font>(letters and numbers count 1, characters and punctuation count 2)<br />     * 计算字符串的字节长度(字母数字计1,汉字及标点计2)     *     * @param strintg     * @return     */    private int byteLength(String strintg) {        int count = 0;        for (int i = 0; i < strintg.length(); i++) {            if (Integer.toHexString(strintg.charAt(i)).length() == 4) {                count += 2;            } else {                count++;            }        }        return count;    }    /**     * According to a specified length, omitting some characters strings.<br />     * 按指定长度,省略字符串部分字符<br />     * for example<br />     * following call method <font color="blue">omitString(string,30)</font><br />     * When the string is full of Chinese characters <br />     * If the string is "中文字符串,中文字符串,中文字符串,中文字符串,中文字符串,中文字符串,中文字符串"<br />     * output "中文字符串,中文字符串,中文 ..."<br /><br />     * When the string is all in English <br />     * If the string is "english string english string,english string,english string,english string"<br />     * output "english string,english stri..."<br /><br />     * When the string contains both English and Chinese characters <br />     * If the string is "中文 and english,english and 中文,中文 and english,"<br />     * output "english and 中文,中文 and e..."<br></br>     * After being intercepted string displayed on the page out of the effect is the same (string length)<br /><br />     * 被截取后的字符串,在页面上显示出来的效果是相同的(字符串长度)     * @param string String intercepted 被截取的字符串     * @param length Intercept length 截取长度     * @return String after interception 截取后的字符串     */    public String omitString(String string, int length) {        StringBuffer sb = new StringBuffer();        if (byteLength(string) > length) {            int count = 0;            for (int i = 0; i < string.length(); i++) {                char temp = string.charAt(i);                if (Integer.toHexString(temp).length() == 4) {                    count += 2;                } else {                    count++;                }                if (count < length - 3) {                    sb.append(temp);                }                if (count == length - 3) {                    sb.append(temp);                    break;                }                if (count > length - 3) {                    sb.append(" ");                    break;                }            }            sb.append("...");        } else {            sb.append(string);        }        return sb.toString();    }

解决方法二

在页面上使用JavaScript来解决

    <script type="text/javascript">        $(function(){            $(".cmsgr-d3").each(function(i){                var divH = $(this).height();                var $p = $("p", $(this)).eq(0);                while ($p.outerHeight() > divH) {                    $p.text($p.text().replace(/(\s)*([a-zA-Z0-9]+|\W)(\.\.\.)?$/, "..."));                };            });        })    </script>    <style type="text/css">        .cmsgr-d3{height:60px;overflow: hidden;}        .cmsgr-d3 p{}    </style>
CSS的宽度,得根据你显示的要求设置

<div class="cmsgr-d3">      <p>       <s:property value="content" />       </p>   </div>


原创粉丝点击