首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Struts2 截字符串长度 除开字符串空格

2013-09-30 
Struts2 截字符串长度 去掉字符串空格package com.gwideal.jxwpgsh.utilimport java.util.ArrayListimpo

Struts2 截字符串长度 去掉字符串空格

package com.gwideal.jxwpgsh.util;import java.util.ArrayList;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.tagext.BodyTagSupport;@SuppressWarnings("serial")public class StringOmitTag extends BodyTagSupport {private int maxLength;private String value;private String suffix;public int getMaxLength() {return maxLength;}public void setMaxLength(int maxLength) {this.maxLength = maxLength;}public String getSuffix() {return suffix;}public void setSuffix(String suffix) {this.suffix = suffix;}public String getValue() {return value;}public void setValue(String value) {this.value = value;}@Override public int doAfterBody() throws JspException { value=bodyContent.getString(); return SKIP_BODY; }public int doEndTag ()throws JspException {JspWriter out = pageContext.getOut();if(null==value){value = "";}try {/*old:按照长度截取if(value.length()>maxLength){value=value.substring(0, maxLength);if(suffix!=null || !suffix.equals("")){value+=suffix;}}*/int vLen = value.getBytes().length;if(vLen>maxLength){value=getSubString(value, (maxLength));if(suffix!=null || !suffix.equals("")){value+=suffix;}}out.print(value);} catch (Exception e) {e.printStackTrace();}return EVAL_PAGE; } public static String getSubString(String oriStr, int subStrLen) { String value=""; if (oriStr==null || subStrLen <= 0) { // return new String[0];//返回数组 return new String(); } ArrayList list = new ArrayList(); byte[] subStrBytes = oriStr.getBytes(); int subStrIndex = 0; int index = 0; while (subStrIndex < subStrBytes.length) { int len = subStrLen; if (subStrIndex + len > subStrBytes.length) { len = subStrBytes.length - subStrIndex; } String subStr = new String(subStrBytes, subStrIndex, len); if (subStr.charAt(subStr.length() - 1) != oriStr.charAt(index + subStr.length() - 1)) { subStr = subStr.substring(0, subStr.length() - 1); len = subStr.getBytes().length; } if(len <= 0){ //return new String[0];//返回数组 return new String(); } subStrIndex += len; index += subStr.length(); list.add(subStr); value = subStr; break; } //String[] subStrs = (String[]) list.toArray(new String[list.size()]); //return subStrs; return value; }}

?

?

?

1 楼 ifox 2012-05-10   鸡动。帮了我一个大忙。谢谢大哥咯

热点排行