Solved

How to Modify Function to Trim a String by Paragraphs Instead of Number of Characters?

Posted on 2004-04-28
6
624 Views
Last Modified: 2012-06-27
I have a function to replace the first x number of characters in a string and not cut them in the middle of words.  Works great.  Question, how can iit be modified to only return, for example, the first two paragraphs (after the </p>)? Running W2K, ASP 3.0.  Here's the code:

In an include file:

FUNCTION TrimString(strString, intLength)
dim a
dim arystring
strString = Replace(strString, vbCrLf, "")
  If Len(strString) > intLength Then
    strString = Left(strString, intLength)
    aryString = Split(strString, " ")
    strString = ""
    For a = 0 to UBound(aryString)-1
      strString = strString & " " & aryString(a)
    Next
    strString = Trim(strString)
    strString = strString & " ..."
  End If
  TrimString = strString
END FUNCTION

----------
to call it on the page

intLength = 300
strString = TrimString(strString, intLength)

Thanks in advance!
0
Comment
Question by:raudi
6 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 10943337
So you want just the first two paragraphs rather than the first 300 characters (not splitting the word). Is that correct?

Fritz the Blank
0
 
LVL 46

Assisted Solution

by:fritz_the_blank
fritz_the_blank earned 50 total points
ID: 10943381
The idea here is to split your string based on either the VBCrLf or the Chr(10) as the case may be. You will have to try both methods:

FUNCTION TrimString(strString, intParagraphs)
  arrParagraphs = Split(strString, VBCrLf)
   for i=0 to intParagraphs
    strStringReturned = arrParagraphs(i) & VbCrLf
   next
   TrimString = strStringReturned
end function

or:

FUNCTION TrimString(strString, intParagraphs)
  arrParagraphs = Split(strString, Chr(10) )
   for i=0 to intParagraphs
    strStringReturned = arrParagraphs(i) & Chr(10)
   next
   TrimString = strStringReturned
end function
0
 
LVL 21

Accepted Solution

by:
ap_sajith earned 400 total points
ID: 10944327
If you're looking to trim html text, then you can use the following code.

<%
strInput="<P>First para</P><P>second para</P><P>Third para</P><P>Fourth para</P><P>Fifth para</P>"


function TrimText(byVal str)
Dim iPos,iend
If str<>"" Then
      iPos=Instr(1,str,"</p>",1)
      If iPos=0 Then
            TrimText=str
            Exit Function
      End If
      iend=Instr(iPos+1,str,"</P>",1)
      If iend=0 Then
            TrimText = Left(str,iPos+3)
      Else
            TrimText = Left(str,iEnd+3)
      End If
Else
      TrimText = Left(str,iEnd+3)=""
End If
end function
Response.write TrimText(strInput)
%>

cheers!!

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Assisted Solution

by:chisholmd
chisholmd earned 50 total points
ID: 10946217
You could try to use the regexp object.

You should be able to form a pattern like this:
match <p> then any number of characters of any type ending in </p>
I thin kit would look something like this:

set re = new regexp
re.global=true
re.ignorecase=true
re.pattern= "\<p>.*\</p>?"
set matchSet = re.execute(strToTest)

that should give you a match set collection of all <P>'s then you just use the first two matches if you only want the first two.

There is more regexp info at:
http://www.juicystudio.com/tutorial/asp/regexp.asp

building patterns can be a heck of job to get right but regular expressions are so darn useful it's worth the practice.





0
 

Author Comment

by:raudi
ID: 10949228
I couldn't get fritz the blank's functions to work, but I have so little time to tinker but I gave him some points for speedy response.  

The function from ap_sajith worked the first time out, checks for empty strings and lets me meet a deadline so he gets most points

Some points were also given chisholmd for the regexp object and link (which I once had bookmarked, but lost) which is what I'd like to try, but have no time to pursue.

Tempus fugit...thanks for you help.
R
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 10950279
You're welcome and good luck with the project.

FtB
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now