[dba-VB] Count of specific character

Shamil Salakhetdinov shamil at users.mns.ru
Tue Jul 24 14:54:44 CDT 2007


Hello John,

You can use System.Text.RegularExpressions (see for details in MSDN) - here
is a C# sample:

// count 'a' char occurrences
string testString = "One car red car blue car";
MatchCollection matches = Regex.Matches(testString, "[a]");
Console.WriteLine(matches.Count.ToString());

Please take into account that Regex is not "lightning fast" - I mean experts
do not recommend to rely on its speed to investigate/parse large amounts of
text data in short time...

If time is not an issue then Regex can do in a few (one!?) line of code many
things, which manually written will take many more code lines. Although as I
noted above manually written custom code could be (much) quicker than Regex
to "pump through" large amounts of text data...

--
Shamil
 

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of jwcolby
Sent: Tuesday, July 24, 2007 11:33 PM
To: dba-vb at databaseadvisors.com
Subject: [dba-VB] Count of specific character

Is there any single function that will return a count of a specific
character in a string?

I have a situation where (apparently) the header of a CSV file has the same
field name twice.  I am trying to determine what is going on, so I want to
count the field separator characters in the header and the first line of
real data to see if there are more fields in the header than in the data or
if something else is going on.

John W. Colby
Colby Consulting
www.ColbyConsulting.com 

_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com




More information about the dba-VB mailing list