Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying Limited characters or Words 1

Status
Not open for further replies.

kizziebutler

Programmer
Apr 28, 2005
81
GB
I am developing a shopping cart and I noticed on some shopping carts that the item description has only so many characters displayed, such as "Health Books more .....". The more .... would be the hyperlink to display the full description of the item. An example of the code I am using is:

out.print("<font style=\"font-size: 10pt; color: #000000\">"+toHTML(flddescription)+"&nbsp;</font>");

This prints out the whole description of the item.

Any advice would be much appreciated
 
Hi,

If you are using commons-lang-2.0.jar in your classpath then you can use StringUtils.abbrivate(String srt, int width).

If not you can copy below code and call abbrivate method.

Code:
public static String abbreviate(String str, int maxWidth) {
        return abbreviate(str, 0, maxWidth);
    }


 public static String abbreviate(String str, int offset, int maxWidth) {
        if (str == null) {
            return null;
        }
        if (maxWidth < 4) {
            throw new IllegalArgumentException("Minimum abbreviation width is 4");
        }
        if (str.length() <= maxWidth) {
            return str;
        }
        if (offset > str.length()) {
            offset = str.length();
        }
        if ((str.length() - offset) < (maxWidth - 3)) {
            offset = str.length() - (maxWidth - 3);
        }
        if (offset <= 4) {
            return str.substring(0, maxWidth - 3) + "...";
        }
        if (maxWidth < 7) {
            throw new IllegalArgumentException("Minimum abbreviation width with offset is 7");
        }
        if ((offset + (maxWidth - 3)) < str.length()) {
            return "..." + abbreviate(str.substring(offset), maxWidth - 3);
        }
        return "..." + str.substring(str.length() - (maxWidth - 3));
    }

Example of how to use

Code:
String str ="This is a simple test for abbrivate"
System.out.println("out put ::"abbreviate(str,10));

out put :: This is...

Cheers
venu
 
I have imported the code but I am having problems with the abbreviate code. My the description is reserved as fldnote and would be displayed as:

out.print("<font>"toHTML(fldnotes)+"</font>"); but when I am having problems with the abbreviate code as where this would site on this coding.
 
Code:
out.print("<font>" + toHTML(abbreviate(fldnotes, 10)) + "</font>");
perhaps?

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Hi,

The best way would be to create a java class and import it to the jsp page and use the method, as you can reuse it in other pages as well with out writing the java code again.

OR

If you are sure that you are using it in only one jsp page and does not mind writing methods in a jsp page you can copy the code into the jsp page were you want to use this method.

Below is the example of how use in the jsp page, But I recommend it to be a java class.

Code:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%!
    static String abbreviate(String str, int maxWidth) {
        return abbreviate(str, 0, maxWidth);
    }

    static String abbreviate(String str, int offset, int maxWidth) {
        if (str == null) {
            return null;
        }
        if (maxWidth < 4) {
            throw new IllegalArgumentException("Minimum abbreviation width is 4");
        }
        if (str.length() <= maxWidth) {
            return str;
        }
        if (offset > str.length()) {
            offset = str.length();
        }
        if ((str.length() - offset) < (maxWidth - 3)) {
            offset = str.length() - (maxWidth - 3);
        }
        if (offset <= 4) {
            return str.substring(0, maxWidth - 3) + "...";
        }
        if (maxWidth < 7) {
            throw new IllegalArgumentException("Minimum abbreviation width with offset is 7");
        }
        if ((offset + (maxWidth - 3)) < str.length()) {
            return "..." + abbreviate(str.substring(offset), maxWidth - 3);
        }
        return "..." + str.substring(str.length() - (maxWidth - 3));
    }
%>

<%
    String fldnotes ="This is a simple test for abbreviate";
%>
<html>
  <head><title>Simple jsp page</title></head>
  <body>
      <%out.print("<font>"+abbreviate(fldnotes,10)+"</font>");%>
  </body>
</html>

replace the above out.print with timw code.


Cheers
Venu
 
I am getting some good display, I will work on it. Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top