Wrox Programmer Forums
|
Classic ASP Components Discussions specific to components in ASP 3.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Components section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 20th, 2006, 12:53 PM
Authorized User
 
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default InStr() repeat instances

Hi There! I'm hoping someone can help me with a little problem I'm having trouble trying to work out myself.

I've written a little function which checks a string for carriage returns in order to display only the first paragraph of a story which works as follows.

Function FirstParagraph(strStory)
  SentanceCount = InStr(strStory,vbCrLf)
  Response.Write Left(strStory,SentanceCount)
End Function

This works fine for returning the first paragraph however, now I require the first TWO paragraphs. Essentially, I need InStr to return the number of characters from the start of the string up to where the secod instance of vbCrLf occurs in the string.

Any ideas? Thanks in advance :)
 
Old November 20th, 2006, 02:02 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The instr function accepts a parameter for the starting position.
You can use the value of the position of the first match as the starting point for the second match, adding two characters to move you past the vbcrlf:

dim secondStart
secondStart = SentanceCount + 2
SecondVbCrLf = Instr(secondStart, strStory, vbCrLf)

Note that if the SecondVbCrLf is zero, then there is no second vbcrlf

After getting the position of the second vbcrlf you can use the Mid funtion to get the second paragraph

Response.Write Mid(strStory, secondStart, (SecondVbCrLf - secondStart - 1))

Of course, you have to be prepared for the case where there is no second vbCrLf, and handle that properly.

However, there are other ways to do this as well.
One would be to use the Split function as follows:

dim arr
arr = Split(strStory, vbcrlf)
Response.Write arr(1) ' This returns the SECOND paragraph, since the array is zero based.

Another is to use regular expressions, which is a bit too complicated to cover easily here - but is something you will want to learn about at some point.

Woody Z http://www.learntoprogramnow.com
 
Old November 20th, 2006, 11:31 PM
Authorized User
 
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by woodyz
 The instr function accepts a parameter for the starting position.
You can use the value of the position of the first match as the starting point for the second match, adding two characters to move you past the vbcrlf:

dim secondStart
secondStart = SentanceCount + 2
SecondVbCrLf = Instr(secondStart, strStory, vbCrLf)

Note that if the SecondVbCrLf is zero, then there is no second vbcrlf
Thanks for your response Woodyz. I seem to be having a little bit of trouble with the start parameter of the InStr function. I don't think that it's actually counting the number of char's in the string between the start and the next instance of vbCrLf as when I return the output to the page I only get half of the second sentence.

Similar with the Split command, arr(0) returns the first par, but arr(1) doesn't return anything... although I know that there is a second par... in fact there are at least 10 par's.

I'm baffled!

 
Old November 21st, 2006, 03:27 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, show us the code you are working with.
These are fundamental VB tasks, and they work every time... so what is it you are not considering?
The split function, if it returns the first paragraph, should definately have at least the second paragraph if there is any text at all after the first vbCrLf.
That is just the way it is.
Using the instr function you are at least finding the first vbcrlf, and it is probably the length part of the mid function that is causing the problem.

My advice is to make a few Instr and Mid tests to learn how to use these properly.

Woody Z http://www.learntoprogramnow.com
 
Old November 21st, 2006, 03:39 AM
Authorized User
 
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by woodyz
 Well, show us the code you are working with.
You are right, it was the Mid() which was causing the issue. I had no trouble detecting the first paragraph using the InStr function to find the vbCrLf and then trim it out using Left().

What I found is that I actually need to add 6 to the value of the first InStr in order to be able to chop out the 2nd paragraph.

Here is what I ended up with and so far so good... fingers crossed!!


Function FirstParagraph(strStory,strParagraphCount)
  Dim strStringCount, strStringCountPar2
  strStringCount = InStr(strStory,vbCrLf)

  If strParagraphCount = "2" then
    strStringCount = strStringCount + 6
    strStringCountPar2 = InStr(strStringCount,strStory,vbCrLf)
    Response.Write Replace(Left(strStory,strStringCountPar2),vbCrLf," <br/>")
  Else
    Response.Write Left(strStory,strStringCount)
  End If
End Function
 
Old November 21st, 2006, 11:48 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am guessing that is some cases you have more several vbCrLf's together separating the paragraphs. It isn't uncommon to have a pair of linefeeds to visually place a blank line between paragraphs. If you have to add 6 to the strStringCount, then you probably have at least 2 sets of vbCrLf character pairs - each one takes 2 characters, so 2 x 2 = 4. If there are 3 vbCrLf then 2 x 3 = 6. So... you have a bit to think about there, and I would recomment a little experiment. Just to see what you do have, do a replace on the vbCrLf's in your string replacing them with something very visible, like "*****|||||" or something like that. It will make it easy to see all the vbCrLf's there are, and help you understand why the Split or Instr/Mid work you did didn't seem to give the correct result. It's a good learning opportunity.

Woody Z http://www.learntoprogramnow.com
 
Old November 23rd, 2006, 11:28 AM
Authorized User
 
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks woodyz... I'll take your advice and try your suggestion.

Although my code works, I'm very curious to know why it works because I shouldn't have had to add 6 to get the desired result.

I suspect the software which generates the data placed into the SQL Server (which I am extracting and displaying) is doing something really strange with CR's and LF's in between paragraphs.

 
Old November 23rd, 2006, 11:55 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by spinout
 Thanks woodyz... I'll take your advice and try your suggestion.

Although my code works, I'm very curious to know why it works because I shouldn't have had to add 6 to get the desired result.

I suspect the software which generates the data placed into the SQL Server (which I am extracting and displaying) is doing something really strange with CR's and LF's in between paragraphs.
It is good to see what your data actually contains. One way to do this is with a binary viewer - this will allow you to see the character codes in your text, whether they are displayed (like letters and numbers) or not (like tabs, cr, lf, space, etc).

Woody Z http://www.learntoprogramnow.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Oracle Instr ermutigen BOOK: SQL Functions Programmer's Reference 1 May 31st, 2009 12:04 AM
CHARINDEX instead of Instr... mircea Classic ASP Databases 7 July 19th, 2004 01:47 PM
help on functions instr.... kyootepuffy Classic ASP Basics 2 September 19th, 2003 10:01 AM
instr function Beulah ASP.NET 1.0 and 1.1 Basics 1 September 18th, 2003 01:54 AM
InStr() in Access 2000 fordrs3 Access VBA 1 July 8th, 2003 05:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.