Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 6th, 2004, 11:23 AM
Authorized User
 
Join Date: Jun 2003
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default Count all occurences of a character in a string

Hi....

There is a string method to count the total number of a specified character in a string.
EX: count the total of (*) in a string
*** Test ***

Thanks....
 
Old December 28th, 2004, 11:23 AM
Registered User
 
Join Date: Dec 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Code:
string array="somestring";
char check='a';
int count=0;
CharEnumerator ce=array.GetEnumerator();
    while(ce.MoveNext())
    {
        if(check==ce.Current)
        {
            count++;
        }
    }
 
Old February 9th, 2007, 01:52 PM
Authorized User
 
Join Date: Apr 2005
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Code:
Function CountOccurences(sInputstring As String, sSearchstring As String) As Long
'0:48:14 - saturday 10.02.2007 - Vijay Saraff
'Counts the number of occurences of a particular string pattern in an input string
'works by splitting on string pattern and counting the # of splits
'dont know if this is the fastest/most efficient way of doing this
CountOccurences = UBound(Split(sInputstring, sSearchstring))
End Function





Similar Threads
Thread Thread Starter Forum Replies Last Post
count occurrence of a character in a string surya Javascript How-To 6 May 7th, 2012 04:23 PM
Count unique occurences Umasriram2 Excel VBA 2 May 28th, 2008 05:10 AM
Count string occurences across recordset goldstein SQL Server 2005 1 July 9th, 2007 06:47 PM
Count character value using Stored Procedure itsurshailesh SQL Language 1 November 14th, 2006 01:31 AM
JSP: Count character in text file life4fun7 JSP Basics 1 June 7th, 2006 09:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.