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

如果用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>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值