按字节截取字符串的jsp自定义标签
tld 定义如下:
<!-- 按字节数截取字符串 -->
<tag>
<name>sliceByte</name>
<tag-class>...</tag-class>
<body-content>empty</body-content>
<attribute>
<name>baseStr</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>byteCount</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>escapeXml</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>addEllipsis</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>variable</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
java代码实现如下:
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import org.apache.commons.lang.StringEscapeUtils;
/**
* 按字节截取字符串标签 (非按字符截取)
*
* @author chengkai
*
*/
public class SliceByte extends SimpleTagSupport {
/**
* 原始字符串
*/
private String baseStr;
/**
* 截取数量
*/
private long byteCount;
/**
* 是否转义
*/
private boolean escapeXml;
/**
* 是否在进行截取后添加...
*/
private boolean addEllipsis;
/**
* 将截取后的结果保存进指定变量
*/
private String variable;
public String getBaseStr() {
return baseStr;
}
public void setBaseStr(String baseStr) {
this.baseStr = baseStr;
}
public long getByteCount() {
return byteCount;
}
public void setByteCount(long byteCount) {
this.byteCount = byteCount;
}
public boolean isEscapeXml() {
return escapeXml;
}
public void setEscapeXml(boolean escapeXml) {
this.escapeXml = escapeXml;
}
public boolean isAddEllipsis() {
return addEllipsis;
}
public void setAddEllipsis(boolean addEllipsis) {
this.addEllipsis = addEllipsis;
}
public String getVariable() {
return variable;
}
public void setVariable(String variable) {
this.variable = variable;
}
@Override
public void doTag() throws JspException, IOException {
JspWriter out = this.getJspContext().getOut();
String resultStr = this.substringByBytes(this.baseStr, 0, byteCount);
resultStr = resultStr == null ? "" : resultStr;
if (addEllipsis && baseStr != null && baseStr.length() != resultStr.length()) {
resultStr += "...";
}
resultStr = escapeXml ? this.html(resultStr) : resultStr;
if (this.variable != null) {
this.getJspContext().setAttribute(variable, resultStr);
} else {
out.print(resultStr);
}
}
/**
* 根据字节截取字符串
*
* @param baseString
* 原始字符串
* @param offset
* 字节偏移索引 (包含)
* @param count
* 要截取的字节数
* @return 返回截取后的字符串
*/
public String substringByBytes(String baseString, long offset, long count) {
if (baseString == null) {
return null;
}
offset = offset < 0 ? 0 : offset;
count = count < 0 ? 0 : count;
char[] chr = baseString.toCharArray();
StringBuffer resultStr = new StringBuffer();
for (int index = 0, bytecnt = 0, bytenum = 0; index < chr.length; index++) {
int currentBC = String.valueOf(chr[index]).getBytes().length;
bytecnt += currentBC;
if (bytecnt >= offset) {
bytenum += currentBC;
if (bytenum <= count) {
resultStr.append(chr[index]);
} else {
break;
}
}
}
return resultStr.toString();
}
private String html(String content) {
return StringEscapeUtils.escapeXml(content);
}
}
分享到:
相关推荐
C# 按照字节长度截取字符串,可以截取任意字节长度得字符串,使用了一点正则表达式。
asp.net c# String类添加按字节截取字符串的扩展方法
字符串截取,按字节截取字符串,截取字符串帮助类
本文实例讲述了Go语言按字节截取字符串的方法。分享给大家供大家参考。具体实现方法如下: 代码如下:// 按字节截取字符串 utf-8不乱码 func SubstrByByte(str string, length int) string { bs := []byte(str)[:...
主要介绍了JAVA如何按字节截取字符串,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
实现一个按字节来截取字符串的方法,功能类似于string类的substring方法,String类是按字符截取 的,例如"中国abc".substring(1,3),将返回“国a”。这里 要求按字节截取,一个英文字符当一个字节,一个中文字符当两...
关于java按字节截取带有汉字的字符串的解法.不错的一个程序。我也是从高手那拿来的,觉得不错,传上来给感兴趣的朋友学习学习。
7. 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉...
按字节长度截取字符串(支持截取带HTML代码样式的字符串) ,补全不完整的HTML代码
一般截取字符串是按照字符截取的,现在这个代码是按照字节截取字符
int iCnt = 0; int i_index; int i_len; System.Text.StringBuilder strRet = new System.Text.StringBuilder(); i_len = strVal.Length; byte[] chrbyte;... System.Text.Encoding encoding = System.Text....
用SQL语句截取字符串, string substr ( string string, int start [, int length]) 参数1:处理字符串 参数2:截取的起始位置(第一个字符是从0开始) 参数3:截取的字符数量
字符串按照指定的字节数来截取
最近需要用到按字节数截取字符串。在网上找了很多方法。 Encoding.Default.GetString采用的Default Encoding.UTF8.GetBytes采用的是utf-8编码。这样当然是乱码。尤其出现中文时候。 对这类数据处理当然要用统一的...
做过大型网站前台的Web开发程序员最头疼的事情就是前台首页的布局,这其中最最头疼的就是标题字符的截取的问题,虽然有fn:substring,但表现率以及对中文的支持均不够完善,其实最终还是归结为客户要求太高了,不过...
截取含有中英文的字符串,按字节进行截取,当出现汉字时不能截取半个汉字要把汉字补全。
asp.net截取字符串方法 使用字节精确计算 计算空格 直接复制使用 简单
java编写一个截取字符串的函数,输入为一个字符串和字节数
下面小编就为大家带来一篇java按字节截取带有汉字的字符串的解法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧