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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

limit number of words dsplayed

Status
Not open for further replies.

thehms

Programmer
Jul 3, 2003
58
AE
hello all....

i have a query from a database and the result contains a lot of text, i want to show only say the first 20 words and then have it followed by 3 dots (...) how can i do this...???

Thank You,
The HMS
 
You could just pick a set amount of characters: eg 120.

Code:
strResult = "blablabla" 'instead pile your query result into the variable

'trim it if too long
if Len(strResult) > 120 then
  strResult = Left(strResult, 120) & "..."
end if

You can do it with words, but it is more complicated (and this could be good enough)

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
If you truly want to doa word limit, then you will need to define what is a word. If we go by a simply definition then it is a bunch of non-space characters followed by a space.

The easiest way (I think) to handle this would be to write a function to accept a string and a number and pass back another string that contains up tothat number words followed by a ...

Code:
Function WordTrim(origStr,numWords)
   Dim words
   words = Split(origStr," ")

   Dim count 
   If UBound(words) + 1 <= numWords Then
      'we don't need to do anything, the sentance is shorter than numWords
      WordTrim = origStr
   Else
      'we need to trim words off
      Do Until count >= numWords
         WordTrim = WordTrim & words(count)
         count = count + 1
      Loop
      WordTrim = WordTrim & &quot;...&quot;
   End If
End Function

I think that may do it. I ignored the case of having two spaces, oneafter another, but that could be taken care of rather simply by either replacing that occurence in the very beginning or by putting in a second counter to compare against numWords and not incrementing it's count when you hit a null word in the array.

-Tarwn

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
thank you guys for your help.....

i really appreciate it...:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top