Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Text Preview in ASP

0.00/5 (No votes)
27 Sep 2000 1  
How to generate short preview of the text in ASP.

Introduction

The task of displaying large amounts of text in ASP applications seems to become a pretty popular topic lately. Specifically, I have come across the discussions on what is the best way to display first so many characters of the text at least a couple of times in the past month. I am talking about the feature similar to what CodeProject's forum scripts do when they are in the message preview mode. In addition to displaying a list of the message subjects or the names of the documents (more generic case) you want to give the user a pick at the content of the message or the document.

It is a really useful feature, because it gives the user a better idea about each message (document) and in most cases stops the user from clicking on each item just to find out more about it. Therefore, it's not only convenient for the user, but also reduces the number of requests to your web server.

The "Left" solution or "Quick and Dirty"

So we have decided that the preview of the document is the way we want to go. How do we do that?
The first thing that comes to mind is VBScript's very own Left() function. Say we want to limit the size of our preview portion of the document to 500 characters:

' Show first 500 characters of the text
Response.Write Left( strText, 500 )

It doesn't get any easier, it's very fast performance-wise, but it also has a downside - it's not very user-friendly. I mean, where is the friendly "..." that will tell user that there is more to this document that we have displayed?
That is easy to fix:

' Show first 500 characters of the text
Response.Write Left( strText, 496 ) & " ..."
Did you notice that I've used 496 instead of 500 as a second parameter of Left()? Well, we've decided to show only 500 chars all together, so we better stick to it. :-)
Here comes the next problem: what if one of the documents only has 200 characters in it? Our friendly "..." has just become not so friendly. User clicks on the document to read some more just to find out that there is nothing else there beside what he/she has already seen. Fortunately, this problem is easily fixed as well:

' Check if size of text is within 500 chars limit
If Len(strText) > 500 Then
	' Show first 500 characters of the text
	Response.Write Left( strText, 496 ) & " ..."
Else
	' Show all the text
	Response.Write strText
End If

There is only one more problem left. With this approach you will almost always cut the text off somewhere in the middle of the word. I don't know about you, but it doesn't look good in my opinion. So the rest of this article should tell you how to solve it.

The "Right" solution and why I will never use it

Someone has posted an article recently on the couple of ASP sites that deals with this same problem. The author has called his solution "The Right Way" of taking the first portion of the text. Let's take a look at the proposed solution:

  1. Remove all the line breaks from the text (strTemp = Replace(strText, vbCrLf, "")).
  2. Check if the length of the text is shorter then your limit.
  3. If it is - display the text without changes.
  4. If it's not:
    1. Take first portion of the text up to the limit (strTemp = Left(strTemp, 500)).
    2. Create an array of all the words in the text (strTemp = Split(strTemp, " ")).
    3. Use For ... Next loop to add words (without the last word) to the resulting string.
    4. Append " ..." to the resulting string.

I have to say that this algorithm will work correctly in most of the cases. But here are the few problems with it as I see it:

  1. Creating an array of words and then concatenating them in the loop is very slow, inefficient and resource hungry process. I've done some performance testing and depending on the set of data I was working with this algorithm produced from 2 to 8 times slower results than the solution proposed below in this article.
  2. Removing the line breaks is only a good idea if you replace them with spaces. Otherwise, you are running into a risk of attaching the last word of the line to the first word of the next line without any space in between. At the same time, this whole step could be skipped since the browser will ignore the line breaks anyway and all we potentially loose is a few characters of the meaningful text.
  3. In case if the text in question has no spaces up to the limit (somebody's SpaceBar stopped working) - all you will see is " ...".
  4. Unconditional appending of " ..." at the end can potentially produce string longer than the limit we set. For example, if 500th character happened to be a space the resulting string would be 503 characters long. It's probably not a big deal if you just use this function to display the text, but what if you've decided to use this function to store your previews to the database ...

Adding a couple of If's here and there could solve some of the above problems. Unfortunately, it will not solve the performance problem of this approach. This is why we should look for an alternative solution.

The Proposed solution

A few comments about the proposed solution:

  • TextPreview function will not append " ..." at the end if the number of characters requested is less than 5. I have a hard time imagining why would anyone want to preview only 4 characters of the text, but I warned you. :-)
  • If no spaces were found up to the "Number of Characters" requested - it will cut the text off at the ("Number of Characters" - 4) and will append " ..." to it.
  • If you would like to replace all the line breaks with spaces you need to uncomment the first line of code in the function. That is only useful when your preview is relatively big or you anticipate a lot of line breaks in the text.

Here is how you could use this function:

' Show first 200 characters of the text, whole words if possible
Response.Write TextPreview( strText, 200 )

And here is the function itself:

' TextPreview function 
'
' Returns first portion of the text up to nChars characters
' Returns only whole words when possible
'
' strText - text
' nChars - number of characters to limit preview to
'
Function TextPreview(strText, nChars)
	Dim nPos

    	' Uncomment next line to replace line breaks with spaces
	' strText = Replace(strText, vbCrLf, " ")

	' Check if it's longer than limit
    	If Len(strText) > nChars And nChars > 4 Then
        	' Find the end of last whole word that we can use
        	nPos = InStrRev(Left(strText, nChars - 3), " ")

        	If nPos > 0 Then
			' Take whole words only 
            		TextPreview = Left(strText, nPos) & "..."
        	Else
			' No spaces were found - take what we can
            		TextPreview = Left(strText, nChars - 4) & " ..."
	        End If
    	Else
		' Take nChars from the text
        	TextPreview = Left(strText, nChars)
    	End If
End Function

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here