When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Thursday, December 07, 2000

Determining if a String has Certain Substrings


There are numerous times when a developer would like to be able to quickly determine if a string contains a particular substring. Fortunately, there is the InStr function in VBScript that can be used to quickly determine whether or not a certain substring is within another string. The InStr function, whose technical documentation can be found here, has the following form:

- continued -

InStr(start, SearchString, SubStringToFind[, compare])

The InStr function returns the starting position of SubStringToFind in SearchString if it is found, else InStr returns a value of zero (0). Therefore, a quick way to check if a string contains a particular substring is to use the following line of code:

Dim strSearchString, strSearchFor
strSearchString = "Now is the time for all good men to..."
strSearchFor = "men"
If InStr(1, strSearchString, strSearchFor) > 0 then
  'We found strSearchFor in strSearchString
Else
  'We didn't find strSearchFor in strSearchString
End If

Fairly simple and straightforward... chances are you've used a similar chunk of code an uncountable number of times yourself in your own ASP pages. However, imagine that we have some string that we want to ensure does not have a large number of substrings? Perhaps we are letting a user enter a value into some textarea form field and we want to make sure that they do not have substrings like <html>, </html>, <body>, and </body>.

Well, we could simply have multiple InStr lines, but that would get annoying fast. Why not create a couple custom functions that will handle this for us in one or two lines of code? Before I delve into the code, let me explain what I am going to attempt to do here:

  • First, I will create a Dictionary object that will contain, as a key, each substring that we do not want to appear in some string. (For more information on the Dictionary object (Scripting.Dictionary), check out Using the Dictionary Object!) Each value of each Dictionary entry will initially be set to False.
  • When I want to determine if a string contains any offending substrings, I will loop through each element of my Dictionary object using InStr to check if the current Dictionary element's key (the offending substring) is found in the string. If it is, I'll adjust that entry's value from False to True.

To assist with this, I've created a number of helper functions. The first one is used to add an offending word to the Dictionary object of substrings that are not allowed:

Sub AddViolation(objDict, strWord)
  'Adds a violation (a substring that is not allowed)
  objDict.Add strWord, False
End Sub

Fairly straightforward, no? The function AddViolation expects two parameters: the Dictionary object of offending substrings and an illegal substring. The function simply adds a new element to the Dictionary object with the key as the offending word and a value of False.

The next helper function, CheckStringForViolations, expects two parameters: the string to check for any offending substrings, and the Dictionary object of illegal substrings. The function returns

Function CheckStringForViolations(strString, objDict)
  'Determines if the string strString has any violations
  Dim bolViolations
  bolViolations = False
    
  Dim strKey
  For Each strKey in objDict
    If InStr(1, strString, strKey, vbTextCompare) > 0 then
      bolViolations = True
      objDict(strKey) = True
    End If
  Next   
    
  CheckStringForViolations = bolViolations  
End Function

The function starts off by assigning False to bolViolations; next, a For Each ... Next loop iterates through the Dictionary object. At each iteration, the current Dictionary object key is checked to see if it is a substring of the passed-in string strString. If it is, bolViolations is set to True and the current Dictionary element's value is set to True. The function concludes by returning the value of bolViolations.

A third and final helper function, ListViolations, returns a string that contains a list of those offending substrings found in the string:

Function ListViolations(objDict)
  'Returns a string of the violations found
  Dim strKey, strViolations
  For Each strKey in objDict
    If objDict(strKey) then
      strViolations = strViolations & strKey & ","
    End If
  Next

  ListViolations = Left(strViolations, Len(strViolations) - 1)
End Function

Now that we've examined these helper functions, it's time to look at them in use! We'll look at this, plus at some major enhancements, in Part 2!

  • Read Part 2


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES