devxlogo

Count Substrings

Count Substrings

This little routine demonstrates how easily you can determine the number of substrings within a string, given any specified separator character(s). Pass the string to be parsed, and the separator, which might be multiple characters long, and DCount returns the number of substrings:

 Public Function DCount(ByVal vData As String, _	SP As String) As Integer	Dim x As Integer	Dim n As Integer	If vData = "" Or SP = "" Then Exit Function	vData = Trim(vData)	n = 1	DCount = 1	Do		x = InStr(n, vData, SP, vbTextCompare)		If x > 1 And x < (Len(vData) - Len(SP)) _			Then			DCount = DCount + 1		End If		n = x + Len(SP)	Loop Until x = 0End Functions = "GTL-00030/22*M121222*C001"cnt = DCount(s ,"*")	->	cnt=3cnt = DCount(s ,"/")	->	cnt=2cnt = DCount(s ,"0")	->	cnt=7
See also  What Are International Payments, And How Do They Work?
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist