JSP simple exercise-omitted long string display

Source: Internet
Author: User

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.