JSP simple exercise-omitted long string display
<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page import = "java. util. *" %>
Long String truncation example<%! Public static String strTruncate (String source, int len, String delim) {// intercept the String function and return the processed String // parameter description: source indicates the String to be truncated, // Len indicates the number of bytes to be truncated // delim indicates the string appended to the truncated string if (source = null) return null; // if the string is null, int start is not processed, stop, byteLen; int alen = source. getBytes (). length; // obtain the number of bytes of the string to be truncated. if (len> 0) {if (alen <= len) {// if it is smaller than the number of bytes to be truncated, return source;} start = stop = byteLen = 0; while (byteLen <= len) {if (source. substring (stop, stop + 1 ). getBytes (). length = 1) {// byteLen + = 1;} else {// byteLen + = 2;} stop ++ ;} stringBuffer sb = new StringBuffer (source. substring (start, stop-1); if (alen> len) {// Add the string sb. append (delim);} return sb. toString () ;}return source ;}%> <% String s1 = new String ("aaaaaaaaaaaaaaaa"); String s2 = new String ("bbbbbbbbbbbbbbbbbbbbbbb "); string s3 = new String ("cccccccccccccccccccc"); out. println ("long string truncation example
"); Out. println (strTruncate (s1, 10,"... ") +"
"); Out. println (strTruncate (s2, 5,"... ") +"
"); Out. println (strTruncate (s3, 6,"... ") +"
"); %>
StrTruncate is used to intercept a string and append it to the end of the processed string with the specified string.
Running result