Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 644
  • Last Modified:

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

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)
    strString = Trim(strString)
    strString = strString & " ..."
  End If
  TrimString = strString

to call it on the page

intLength = 300
strString = TrimString(strString, intLength)

Thanks in advance!
3 Solutions
So you want just the first two paragraphs rather than the first 300 characters (not splitting the word). Is that correct?

Fritz the Blank
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
   TrimString = strStringReturned
end function


FUNCTION TrimString(strString, intParagraphs)
  arrParagraphs = Split(strString, Chr(10) )
   for i=0 to intParagraphs
    strStringReturned = arrParagraphs(i) & Chr(10)
   TrimString = strStringReturned
end function
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
      If iPos=0 Then
            Exit Function
      End If
      If iend=0 Then
            TrimText = Left(str,iPos+3)
            TrimText = Left(str,iEnd+3)
      End If
      TrimText = Left(str,iEnd+3)=""
End If
end function
Response.write TrimText(strInput)


Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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.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:

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

raudiAuthor Commented:
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.
You're welcome and good luck with the project.


Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now