473,569 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

# of occurences of string in another string

What's the most efficient way to get the number of occurences of a certain
string in another string..for instance i'm using the following code right
now...

private int CharacterCounte r(String text,String Character)

{

int count = 0;

for (int i = 0; i < text.Length; i++)

{

if(text.Substri ng(i,1) == Character)

{

count++;

}

}

return count;

}

The problem with this way is it's not the fastest doing big strings multiple
times over the course of a program run. Is there an easier/faster way to do
it using regular expressions?


Nov 16 '05 #1
4 13632
Regular expressions are going to be slower.

I wrote something a while back that stores the offset of all string
occurences within another string. You can see that here:
http://weblogs.asp.net/justin_rogers.../14/89545.aspx
The relavent code is in SplitByString and appears below as well.

while(index < testString.Leng th) {
int indexOf = testString.Inde xOf(split, index);
if ( indexOf != -1 ) {
offsets[offset++] = indexOf;
index = (indexOf+1);
} else {
index = testString.Leng th;
}
}

Now, what about fixing that code up to just count?
int index = 0;
while(index < testString.Leng th) {
int indexOf = testString.Inde xOf(split, index);
if ( indexOf != -1 ) {
offsets[offset++] = indexOf;
index = (indexOf+1);
} else {
index = testString.Leng th;
}
}

"Jason Gleason" <ja***********@ gensurvey.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
What's the most efficient way to get the number of occurences of a certain
string in another string..for instance i'm using the following code right
now...

private int CharacterCounte r(String text,String Character)

{

int count = 0;

for (int i = 0; i < text.Length; i++)

{

if(text.Substri ng(i,1) == Character)

{

count++;

}

}

return count;

}

The problem with this way is it's not the fastest doing big strings multiple
times over the course of a program run. Is there an easier/faster way to do
it using regular expressions?


Nov 16 '05 #2
Okay, I hit the send before done button again...

int index = 0;
int count = 0;

while(index < testString) {
int indexOf = testString.Inde xOf(splitString , index);
if ( indexOf != -1 ) {
count++; index = (indexOf + splitString.Len gth);
} else { index = testString.Leng th; }
}

That should get you the number of occurences correctly.
Also note that an issue in my original code didn't take into
account split string length for purposes of offseting. That
makes a big difference in the output of something like (match
all occurences of (aa) in (aaaa). Normally that should be two,
but my old method would have returned three. I guess that is
a highly ambiguous case.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Justin Rogers" <Ju****@games4d otnet.com> wrote in message
news:Os******** *****@TK2MSFTNG P11.phx.gbl...
Regular expressions are going to be slower.

I wrote something a while back that stores the offset of all string
occurences within another string. You can see that here:
http://weblogs.asp.net/justin_rogers.../14/89545.aspx
The relavent code is in SplitByString and appears below as well.

while(index < testString.Leng th) {
int indexOf = testString.Inde xOf(split, index);
if ( indexOf != -1 ) {
offsets[offset++] = indexOf;
index = (indexOf+1);
} else {
index = testString.Leng th;
}
}

Now, what about fixing that code up to just count?
int index = 0;
while(index < testString.Leng th) {
int indexOf = testString.Inde xOf(split, index);
if ( indexOf != -1 ) {
offsets[offset++] = indexOf;
index = (indexOf+1);
} else {
index = testString.Leng th;
}
}

"Jason Gleason" <ja***********@ gensurvey.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
What's the most efficient way to get the number of occurences of a certain
string in another string..for instance i'm using the following code right
now...

private int CharacterCounte r(String text,String Character)

{

int count = 0;

for (int i = 0; i < text.Length; i++)

{

if(text.Substri ng(i,1) == Character)

{

count++;

}

}

return count;

}

The problem with this way is it's not the fastest doing big strings multiple
times over the course of a program run. Is there an easier/faster way to do
it using regular expressions?



Nov 16 '05 #3
> Jason Gleasonwrote:
Is there an easier/faster way to do
it using regular expressions?


Regular expressions are great fun for your spare time, but in addition
to being cryptic and unmaintainable, they'll kill the performance of
your app.

Nov 16 '05 #4
Jason Gleason wrote:
What's the most efficient way to get the number of occurences of a certain
string in another string..for instance i'm using the following code right
now...

private int CharacterCounte r(String text,String Character)

{

int count = 0;

for (int i = 0; i < text.Length; i++)

{

if(text.Substri ng(i,1) == Character)

{

count++;

}

}

return count;

}

The problem with this way is it's not the fastest doing big strings multiple
times over the course of a program run. Is there an easier/faster way to do
it using regular expressions?


If you're searching for exact matches, regex's will not buy you anything
over a well-designed string search. if you're going to search for
patterns, regex's are a good tool.

If you want to count instances of a single character in a string (like
your example) you might be able to tune it a bit (for example, "text[i]"
is probably faster than "text.Substring (i,1)"), but your basic algorithm
is probably fine - if you're going to search the text string only once.

If you're going to count character instances in the same text string
multiple times, it would probably pay to run through the string once,
tallying up all the counts for the various characters into an array
that's indexed by the character value. Subsequent character counts are
just an indexed array access after that.

If you want to change the search from simply counting instances of a
particular character to counting actual instances of strings, you'll
probably want to do some research on the Boyer-Moore string search
algorithm. The framework's String.IndexOf( ) method might be a good
starting point for you, but I don't think it'll be as fast as a
hand-coded Boyer-Moore, since IndexOf() takes into account culture
information.

Do a net search on Boyer-Moore - it's a not-too-complex, but very, very
good algorithm for performing exact string matches.

--
mikeb
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1591
by: Spondishy | last post by:
This is probably more of a algo question than specifically c# (although a c# solution would be great. I'm trying to parse a string and ensure that at least one occurence of a number of other strings within the main string. These must not overlap. For example, if I had the string... "The cow jumped over the moon" and was looking for...
3
1590
by: M.N.A.Smadi | last post by:
hi; say i have a text file with a string ( say '(XYZ)') and I want to find the number of line where this string has occured. What is the best way to do that? what about if that string was say a 0 with leading and trailing white spaces, would that be any different? thanks moe smadi
4
3355
by: Dameon | last post by:
Hi All, I have a process where I'd like to search the contents of a file(in a dir) for all occurences (or the count of) of a given string. My goal is to focus more on performance, as some of the files could be upwards of 25mb in size and time is important. I don't want to take the route of loading the text of the file into a giant string...
8
5619
by: Daneel | last post by:
Hello! I'm looking for an algorithm which finds all occurences of a bit sequence (e.g., "0001") in a file. This sequence can start at any bit in the file (it is not byte aligned). I have some ideas of how to approach the problem (1) reading file into unsigned char buffer, 2) defining bit structure, 3) comparing the first 4 bits of the...
7
1686
by: shubharamaswamy | last post by:
Reading standard input and printing the number of occurences of each letter in graphic format as follows: a: ..... b: .. . . . z: ... where no. of dots= no.of occurences
2
1955
by: kasala | last post by:
I get an xml document as input from other department. The input xml document i recieve has a particular word "rnx" which should not be there and my system doesn't support it. And there is also attribute xml:lang="EN" in some elements which should not be there. I am enclosing the code for reference. I used to remove the occurences manually...but...
18
3853
by: Neehar | last post by:
Hello For one of the interviews I took recently, I was given an offline programming quiz. In 30 minutes I had to write code in C++ to counts the number of times each unique word appears in a given file. I tried my level best even after the quiz to come up with a solution but cudnt find an efficient one. :( This is what I did.
5
6922
by: Larry | last post by:
Dear all, I'm new to Python. I have a file (an image file actually) that I need to read pixel by pixel. It's an 8-bit integer type. I need to get the statistics like mean, standard deviation, etc., which I know a little bit already from reading numpy module. What I want to know is how to get the number of occurences of numeric element in an...
3
2468
by: SLauren | last post by:
Hi, I am trying to find the records based on a search string which can be anything and can match any of the varchar fields in the table. Right now i am getting those records by using the combinations of "LIKE" and "OR" operators.But as per the requirements i also need to show the number of occurences of the search string that could match...
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8125
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.