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>
- Java 字符串截取, 中文英文截取相同长途
- java String 中文 字符串 截取
- PHP_substr()函数_截取字符串_定义chinesesubstr()函数处理_英文截取_中文截取
- Smarty截取字符串(中文和英文均支持)
- python中从字符串中截取中文和英文字符
- JAVA两个字符串截取相同的最长子字符串
- java 中截取中文字符串的问题
- 疯狂JAVA讲义4.1--截取中文字符串
- php 截取中文字符串
- PHP截取中文字符串
- python截取中文字符串
- 截取中文字符串
- php截取中文字符串
- PHP截取中文字符串
- 中文字符串截取
- PHP截取中文字符串
- php截取中文字符串
- 截取中文字符串
- c# 将数组或集合 转换成以逗号分隔的字符串
- iOS Multithreading: Thread Safety in iOS Applications
- ssh项目下易宝支付的编码问题
- 双机热备常见问题解答
- feof函数,fscanf函数,strtok函数等遇到的小疑惑解决办法
- Java 字符串截取, 中文英文截取相同长途
- 中间件 东方通tongweb的使用
- 从驱动到转行到游戏开发的经验
- Microsoft Office Visio 2010如何创建UML 用例图
- 改变checkbox,使之可以像单选按钮一样使用
- SSG5简易配置命令及忘记登录密码和设备ip地址 的方法
- 如何一个android工程作为另外一个android工程的lib
- glassfish:Resource temporarily unavailable in tsStartJavaThread
- C# WinForm源码收集