Tuesday 2 October 2012

ASP: the InStr function

   


One of the most used functions in ASP is InStr.
In this short article we are going to see how to use it.

The Syntax
The InStr function has the following syntax:
InStr([startOfStr,]stringSearched,search[,compare])
where:
startOfStr is optional and it specifies where the search will begin. By default is set to1 (first character of the string). It is required when compare is used.

stringSearched is the string to be searched. It is required.
search is the string to search inside stringSearched. It is required.
compare is optional and it can be 0 (default) or 1. If 0 (vbBinaryCompare) is used, a binary comparison will be performed, If 1 (vbTextCompare), a textual comparison will be performed. Normally we use the default value (0).

Returned values
The function returns a value which must be evaluated. Let's see what kind of response we can obtain:
  • If stringSearched is "" - InStr returns 0
  • If stringSearched is Null - InStr returns Null
  • If search is "" - InStr returns startOfStr
  • If search is Null - InStr returns Null
  • If search is not found - InStr returns 0
  • If search is found within stringSearched - InStr returns the position at which search is found
  • If startOfStr > Len(stringSearched) - InStr returns 0
It is quite obvious what we can do with InStr. Usually we use it to search a text and see if a specific word is contained inside the text itself. We can use it in order to determine where a specific word is placed inside a long text and, if used together with a loop, we can use it to find where a word is, more than once.

As you can see InStr is an interesting function with many possible use. It depends on what we really need to do, and what is our goal. With a little bit of imagination, we can solve a lot of complicated situations.

If you need more help on it, please use the comments section below... in the meantime happy coding!

2 comments:

  1. i need the code for File Upload in Classic Asp..... please send the code to my mail also....i.e diddi.pavan@gmail.com

    ReplyDelete
    Replies
    1. Wrong post. Search the blog you find it.
      Oh by the way I need lot of money, please send it to my account :-)

      Delete

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.