Max Wanadoo
max.wanadoo at gmail.com
Sun Apr 13 04:08:59 CDT 2008
John,
You may recall the RegExp that I posted a short while ago. You can use this
to get the answer.
This code returns 17:
Function CountRegEx()
Dim strIn As String, lngCount As Long
strIn = "asdf||||||qwert|zxcv|mnbv|qwert|zxcv|mnbv||||||"
lngCount = (RegExpTest("\|", strIn))
MsgBox lngCount
End Function
Here is the original Function
Function RegExpTest(patrn, strng) As Long
' Set reference to Microsoft vbscript regular expressions 5.5
' Cheat Sheet available at http://regexlib.com/CheatSheet.aspx
Dim RegEx As New VBScript_RegExp_55.RegExp
Dim Match, Matches, RetStr ' Create variable.
Set RegEx = New RegExp ' Create a regular expression.
RegEx.Pattern = patrn ' Set pattern.
RegEx.IgnoreCase = True ' Set case insensitivity.
RegEx.Global = True ' Set global applicability.
Set Matches = RegEx.Execute(strng) ' Execute search.
RegExpTest = 0
For Each Match In Matches ' Iterate Matches collection.
RetStr = RetStr & "Match found at position "
RetStr = RetStr & Match.FirstIndex & ". Match Value is '"
RetStr = RetStr & Match.Value & "'." & vbCrLf
RegExpTest = RegExpTest + 1
Next
End Function
HTH
Max
-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Shamil
Salakhetdinov
Sent: Sunday, April 13, 2008 3:30 AM
To: 'Discussion concerning Visual Basic and related programming issues.'
Subject: Re: [dba-VB] Count a character in a string
Hi John,
My bet that this code will be the most speedy in .NET (C# sample):
int count = 0;
for (int index = 0; index < s.Length; index++)
if ((s[index] ^ '|') == 0) count++;
--
Shamil
-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of jwcolby
Sent: Sunday, April 13, 2008 12:24 AM
To: VBA
Subject: [dba-VB] Count a character in a string
I do a lot of CSV stuff. One of my file suppliers gives me files that throw
an error when I try to import them and so I am writing tools to try and
troubleshoot these files. I need to be able to count the times that the
field separator character is contained in a line. The error I am getting
says the line is too long but I have determined that is not the case. I
suspect that perhaps the field separator character is in the line too often.
So, if I have a line that looks like:
asdf|qwert|zxcv|mnbv
Other than just iterating through the string character by character, is
there a way to get a count of the | character in the string?
--
John W. Colby
www.ColbyConsulting.com
_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com
_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com