Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How To find the number of a particular character within a string 4

Status
Not open for further replies.

maupiti

Programmer
Oct 27, 2003
240
US
Access 2003

Is there a string function that will compute the total number of a particular character within a string without
using a string array and a for loop ?

Private Sub Compute_The_Number_Of_Spaces_Within_A_String
Dim A_String As String
A_String = "Hello There Everybody"

'There a 3 spaces within the A-String
End Sub
 
maupiti,
Look at the [tt]Matches[/tt] collection in Regular Expressions (regex).

Hope this helps,
CMP

(GMT-07:00) Mountain Time (US & Canada)
 
Hi CautionMP, and thank you for responding. In Access I went into help but I did not find any "regular expressions", or "regex", or "matches collection
 
Hi, maupiti,

I don't know of a native Access function, but you could try this:
Code:
Public Function GetNumberOfCharacters(strText As String, strChar As String) As Integer
Dim i As Integer
Dim x As Integer
Do
    x = InStr(x + 1, strText, strChar, vbBinaryCompare)
    If x > 0 Then
        i = i + 1
    End If
Loop Until x = 0
GetNumberOfCharacters = i
End Function
HTH,

Ken S.
 
Perhaps:
Code:
Function HowMany(OfThese, InThisText)
HowMany = Len(InThisText) - Len(Replace(InThisText, OfThese, ""))
End Function
 
maupit,
It's not a built in function. You can expose it with objects or you can add a reference to your VBA project (Microsoft VBScript Regular Expressions x.x).

Here is an example without adding the reference.
Code:
Function Count_Spaces_with_RegExp(String_to_test As String) As Long
Dim objRegExp As Object
Dim objMatches
Set objRegExp = CreateObject("VBScript.RegExp")
With objRegExp
  .Global = True
  .Pattern = " "
End With
Set objMatches = objRegExp.Execute(String_to_test)
Count_Spaces_with_RegExp = objMatches.Count
Set objMatches = Nothing
Set objRegExp = Nothing
End Function

Here was the test:
Immediate said:
? Count_Spaces_with_RegExp("Hello There Everybody")
2

Hope this helps,
CMP

(GMT-07:00) Mountain Time (US & Canada)
 
Very good, Remou! And if you add the vbBinaryCompare argument, it accounts for upper/lower case also. Have a twinkler...

Ken S.
 
Remou,
Worth a star for the KISS.

CMP

(GMT-07:00) Mountain Time (US & Canada)
 
That's just ... beautiful, Remou. It brings a tear to my eye.

Sorta reminds me of Perl golf, actually.
 
I'm pretty impressed as well - I read the OP and immediately thought of looping through an array. Keep it simple, stupid (as CautionMP said): Good job Remou!

~Melagan
______
"It's never too late to become what you might have been.
 
Hi CautionMP (Programmer). Is the code that you put in object oriented programming ? The other two method that Eupher and Remou presented is shorter and is much easier to understand at a short glance.

Good job though.
 
maupiti,
Yes, it uses the VBScript Regular Expression Object. It has a bunch of tools that are helpful when pattern matching large amounts of text.

CMP

(GMT-07:00) Mountain Time (US & Canada)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top