Solved

truncate string

Posted on 2006-06-30
7
893 Views
Last Modified: 2012-05-05
HI,

I'm trying to shorten a string from the database to show it as a preview.
I use the left function to achieve this but I have HTML codes for special characters. Like ' for single quotes, ë for ë, etc. The string can also contain a link so the length of the preview isn't always the same and in some cases it returns the start of a bit of code.

  This is just some sample text to show the 'problem&#...
or
  This is just some <A HREF="http://www.valerysuper.be" TARGE...

Is there a simple way to shorten the "output" of the string?
0
Comment
Question by:seloh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Expert Comment

by:FSIFM
ID: 17016448
You could convert it all to HTML before doing the left function

Server.HTMLEncode(rst("Field")) - this should conver the ASCII values to HTML then perform the left function.

FSIFM
0
 
LVL 2

Author Comment

by:seloh
ID: 17016740
I have in the database the following string:

On Saturday 23rd of september 2006 is the final for the &#39;the golden twelve&#39; in domain <A HREF="http://www.vossenberg.be" TARGET="_new" CLASS="body">Vossenberg</A> in Hoog. Tickets can be purchased for &#8364; 85,00 per person (all-in).<BR>&#39;The golden twelve&#39; are the girls that will advance to the final of the miss belgian beauty contest which will be held in the Casino of Knokke.<BR>This final will be on saturday the 21st of october, tickets for the show and gala-dinner are stil available for &#8364; 195,00 per person (all-in).<BR><BR>Details information and the menu can be found on <A HREF="http://www.missbelgianbeauty.be" TARGET="_new" CLASS="body">www.missbelgianbeauty.be</A>, and choose &#39;the golden twelve&#39; or &#39;the final&#39;.<BR><BR>Be quick to order your tickets now that they are still available!

If I use left(RS("news"), 100) &"..." it will return the preview:
On Saturday 23rd of september 2006 is the final for the 'the golden twelve' in domain <A HR...

If I use left(RS("news"), 250) &"..." it will return the preview:
On Saturday 23rd of september 2006 is the final for the 'the golden twelve' in domain Vossenberg in Hoog. Tickets can be purchased for € 85,00 per person (all-in).
&#...

If I use left(RS("news"), 250) &"..." and the string wouldn't contain a link it would return a longer preview the preview:
On Saturday 23rd of september 2006 is the final for the 'the golden twelve' in domain Vossenberg in Hoog. Tickets can be purchased for € 85,00 per person (all-in).
'The golden twelve' are the girls that will advance to the f...

I want the preview the user sees to be of the same length for each newsitem wether it contains links/codes or not and I don't want bits of HTML at the end like in the two first examples above.
How can I achieve this?

(A plus would be if I could also end the preview after a word and not in the middle of a word like in the third example. This is not necesary, would be nice but it isn't a problem.)
0
 
LVL 4

Expert Comment

by:FSIFM
ID: 17016835
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 25

Accepted Solution

by:
kevp75 earned 100 total points
ID: 17018671
I use a function to do this for me.  The function is:
Function TruncateMemo(str, ln)
      Dim i, j, k
      If Len(str) > 0 And Len(str) > ln Then
            k = 1
            Do While k > 0 And k < Len(str)
                  i = InStr(k, str, " ", 1)
                  j = InStr(k, str, vbCrLf, 1)
                  If i < 0 And j < 0 Then ' Not able to truncate
                        TruncateMemo = str
                        Exit Function
                  Else
                        If i > 0 And j > 0 Then
                              If i < j Then
                                    k = i
                              Else
                                    k = j
                              End If
                        ElseIf i > 0 Then
                              k = i
                        ElseIf j > 0 Then
                              k = j
                        End If
                        If k >= ln Then
                              TruncateMemo = Mid(str, 1, k-1) & "  (more ...)"
                              Exit Function
                        Else
                              k = k + 1
                        End If
                  End If
            Loop
      Else
            TruncateMemo = str
      End If
End Function


to use it do:
<%=response.write(TruncateMemo(yourString,250)%>  this will truncate your string to 250 characters, if the length of the string is greater than 250 then it will display (more...) at the end of it.

HTH
0
 
LVL 25

Expert Comment

by:kevp75
ID: 17018678
sorry...there's a typo in my response.write statement.  it should be:
<%=response.write(TruncateMemo(yourString, 250))%>
0
 
LVL 2

Author Comment

by:seloh
ID: 17021150
That just works fantastic! Thanks!!
0
 
LVL 25

Expert Comment

by:kevp75
ID: 17021353
no prob.  Thanks for the grade
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question