Skip to main content
NetworkAdminKB Logo

VBScript Library

Go Search
NetworkAdminKB.com
Knowledge Base
VBScript Library
Utilities
  
NetworkAdminKB.com > VBScript Library > Knowledge Base > Components > Strings > CountStr  

CountStr

Article Content

'********************************************************************

'*

'* Function CountStr

'*

'*   Author: NetworkAdminKB.com

'*  Created: 2004-06-29

'* Modified: 2008-12-06

'*

'* Purpose: Returns the number of times a character/string occurs within

'*            the search string.

'*

'* Input:  strSearch = A string to be searched.

'*          strMatch = A string/Character to search for in strSearch.

'*           blnCase = A boolean indicating case sensitive searches

'*                       True  = Case Sensitive Search

'*                       False = Case Insensitive Search

'*

'* Output:  Returns the number of times a character/string occurs within

'*            the search string.

'*          If there is no match 0 is returned.

'*          Returns Empty if strMatch is Empty

'*

'* Calls:

'* ReturnCaseComparisonValue

'*

'* Changes:

'* 2006-12-23: Complete re-write using basic math.

'* 2008-12-06: Added blnCase to support case sensitive searches

'* 2008-12-06: Added ReturnCaseComparisonValue to validate blnCase

'* 2008-12-06: Added validation that length of strMatch > 0

'********************************************************************

Function CountStr(ByVal strSearch, ByVal strMatch, ByVal blnCase)

  'Version: 2.1 2008-12-06

  Dim intSearch

 

  If Len(strMatch) = 0 Then

    Wscript.Echo "CountStr: strMatch must be one character in length."

    CountStr = Empty

    Exit Function

  End If 'Len(strMatch) = 0

 

  intSearch = ReturnCaseComparisonValue(blnCase)

  'The number of occurances = the original length of the string minus

  '  the length of the string with all occurances removed, divided by the

  '  length of the string that was removed.

  CountStr = (Len(strSearch) - Len(Replace(strSearch,strMatch,"", 1, -1, intSearch))) _

             / Len(strMatch)

End Function 'CountStr

Keywords
String Manipulation 
Related Articles
ReturnCaseComparisonValue.aspx 
Last modified at 12/6/2008 2:58 PM  by TEMPLATE\kbadmin 
 
 NetworkAdminKB.com
 Copyright © 2008 NetworkAdminKB.com, All rights reserved. Terms of Use | Contact US