VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



A CountChar Better than looping through a string

by [])utch[]v[]aster (18 Submissions)
Category: String Manipulation
Compatability: VB Script
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (6 Votes)

It used to be, when I wanted to count the number of times a charector appeared in a string, I would loop through the string letter by letter and keep a count. I have replace my old methodolgies with this one, which also allows you to search for substrings (more than 1-digit long)

Rate A CountChar Better than looping through a string



Public Function CountChar(vText as String, vChar as String, Optional IgnoreCase as Boolean) as Integer

  If IgnoreCase Then 

    vText = LCase$(vText) 

    vChar = LCase$(vChar) 
   End If 

  Dim L as Integer 

  L = Len(vText) 


  vText = Replace$(vText, vChar, "") 

  CountChar = (L - Len(vText)) / Len(vChar) 

End Function

Download this snippet    Add to My Saved Code

A CountChar Better than looping through a string Comments

No comments have been posted about A CountChar Better than looping through a string. Why not be the first to post a comment about A CountChar Better than looping through a string.

Post your comment

Subject:
Message:
0/1000 characters