HOWTO: Search for the Last Occurrence of a SubstringLast reviewed: September 30, 1997Article ID: Q168836 |
The information in this article applies to:
SUMMARYThe InStr() function provides a method to search for the first occurrence of one string (substring) inside another string. However, there is no intrinsic method to search for the last occurrence of a substring. This article provides a sample function written in Basic.
MORE INFORMATIONThere are two main methods of searching for the last occurrence of a substring:
This is because InStr() is highly optimized and many orders of magnitude faster than emulating the functionality via looping through the string using the Mid$() function to extract each character. The code example below demonstrates the second method. WARNING: Microsoft provides code/macro examples for illustration only, without warranty either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. This code is provided 'as is' and Microsoft does not guarantee that the following code can be used in all situations. Microsoft does not support modifications of the code to suit customer requirements for a particular purpose. NOTE: In versions of Basic that don't support the "_" line continuation character, the split lines must be entered on a single line when typing the code into the module.
Step-by-Step Example
REFERENCESMicrosoft Visual Basic online Help topic "InStr." Keywords : VB4ALL VB4WIN vb5all vb5howto VBKBProgramming vbwin GnrlVb kbprg Technology : kbvba Version : WINDOWS:4.0 5.0 Platform : WINDOWS Issue type : kbhowto |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |