473,699 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding occurences of many strings in another

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 "cow", "over", "moon" that would pass as all
phrases are in there at least once with no overlaps.

If I looked for "cow", "jumped", "jumped over" that would fail as
"jumped" and "jumped over" overlap.

Although if the phrase was

"The cow jumped and and she jumped over the moon"

"cow", "jumped", "jumped over" would pass.

Any help appreciated.

Nov 17 '05 #1
8 1596
Hello

You can use the very simple algorithm for it. It will work for all provided
by you cases.

string source = "The cow jumped over the moon";
//string source = "The cow jumped and and she jumped over the moon";
string[] needles = new string[] {"cow", "jumped", "jumped over"};

foreach (string needle in needles)
{
int pos = source.IndexOf( needle);

if (pos > 0)
{
System.Diagnost ics.Debug.Write Line("found: " + needle);

source = source.Remove(p os, needle.Length);
}
}

--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072
"Spondishy" <sp*******@tisc ali.co.uk> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
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 "cow", "over", "moon" that would pass as all
phrases are in there at least once with no overlaps.

If I looked for "cow", "jumped", "jumped over" that would fail as
"jumped" and "jumped over" overlap.

Although if the phrase was

"The cow jumped and and she jumped over the moon"

"cow", "jumped", "jumped over" would pass.

Any help appreciated.

Nov 17 '05 #2
Hello

You can use the very simple algorithm for it. It will work for all provided
by you cases.

string source = "The cow jumped over the moon";
//string source = "The cow jumped and and she jumped over the moon";
string[] needles = new string[] {"cow", "jumped", "jumped over"};

foreach (string needle in needles)
{
int pos = source.IndexOf( needle);

if (pos > 0)
{
System.Diagnost ics.Debug.Write Line("found: " + needle);

source = source.Remove(p os, needle.Length);
}
}

--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072
"Spondishy" <sp*******@tisc ali.co.uk> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
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 "cow", "over", "moon" that would pass as all
phrases are in there at least once with no overlaps.

If I looked for "cow", "jumped", "jumped over" that would fail as
"jumped" and "jumped over" overlap.

Although if the phrase was

"The cow jumped and and she jumped over the moon"

"cow", "jumped", "jumped over" would pass.

Any help appreciated.

Nov 17 '05 #3
Thanks for the quick response. To add more complexity what if the
string was

string source = "The cow jumped over the moon and then jumped higher";
string[] needles = new string[] {"cow", "jumped", "jumped over"};

This should pass because "jumped" and "jumped over" occur in the
original string.

Nov 17 '05 #4
Thanks for the quick response. To add more complexity what if the
string was

string source = "The cow jumped over the moon and then jumped higher";
string[] needles = new string[] {"cow", "jumped", "jumped over"};

This should pass because "jumped" and "jumped over" occur in the
original string.

Nov 17 '05 #5
You can use the following, and even with some modification you could
have it return a list of non-matches instead of bool.
public bool TestString(stri ng[] testForStrings, string testString)
{
System.Collecti ons.Hashtable testHash = new Hashtable();
foreach(string item in testForStrings)
{
bool isThere = (testString.Ind exOf(item) > -1);
testHash.Add(it em, isThere);
if(isThere)
{
testString = testString.Remo ve(testString.I ndexOf(item),
item.Length);
}
}
return !testHash.Conta insValue(false) ;
}

Then I basically, to test, did the following:

string test = "The cow jumped and she jumped over the moon";
string[] items = "cow,jumped,jum ped over".Split(',' );

MessageBox.Show (TestString(ite ms, test).ToString( ));

Nov 17 '05 #6
You can use the following, and even with some modification you could
have it return a list of non-matches instead of bool.
public bool TestString(stri ng[] testForStrings, string testString)
{
System.Collecti ons.Hashtable testHash = new Hashtable();
foreach(string item in testForStrings)
{
bool isThere = (testString.Ind exOf(item) > -1);
testHash.Add(it em, isThere);
if(isThere)
{
testString = testString.Remo ve(testString.I ndexOf(item),
item.Length);
}
}
return !testHash.Conta insValue(false) ;
}

Then I basically, to test, did the following:

string test = "The cow jumped and she jumped over the moon";
string[] items = "cow,jumped,jum ped over".Split(',' );

MessageBox.Show (TestString(ite ms, test).ToString( ));

Nov 17 '05 #7
Ups, the next thing will not work in my sample. It is rather simple, just an
idea.

But, we can make a quick fix of the problem. Just search for longest needles
first.

--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072
"Spondishy" <sp*******@tisc ali.co.uk> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Thanks for the quick response. To add more complexity what if the
string was

string source = "The cow jumped over the moon and then jumped higher";
string[] needles = new string[] {"cow", "jumped", "jumped over"};

This should pass because "jumped" and "jumped over" occur in the
original string.

Nov 17 '05 #8
Ups, the next thing will not work in my sample. It is rather simple, just an
idea.

But, we can make a quick fix of the problem. Just search for longest needles
first.

--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072
"Spondishy" <sp*******@tisc ali.co.uk> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Thanks for the quick response. To add more complexity what if the
string was

string source = "The cow jumped over the moon and then jumped higher";
string[] needles = new string[] {"cow", "jumped", "jumped over"};

This should pass because "jumped" and "jumped over" occur in the
original string.

Nov 17 '05 #9

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

Similar topics

4
6848
by: Travers Naran | last post by:
Here's the basic idea. I have a dictionary of substrings (the substrings stored as keys). I have a list of strings. I want to find out, for each word in the dictionary, how many times the substring occurs non-overlapping occurrences there are in the list of strings. This is the best I could come up with: for j in self.dict.keys(): c = 0 for i in tc.strings: c += i.count(j)
4
6569
by: Victor Engmark | last post by:
When looking for a method to fetch unique elements and counting the number of occurences of each of them, I found quite a lot of gross examples of complex XSL. But after realizing the subtle difference between "." and "current()", I found a neat way of doing the same without keys or generate-id(): <xsl:template match="/"> <!-- Selects all "new" elements --> <xsl:for-each select="//Name"> <!-- Display the element -->
3
1608
by: Gadrin77 | last post by:
<Restaurants> <FastFood> <DriveIn Name="Del Taco"/> <DriveIn Name="McDonalds"/> <DriveIn Name="Bakers"/> <DriveIn Name="Arbys"/> <DriveIn Name="Green Burrito"/> <DriveIn Name="Jack in the Box"/> <DriveIn Name="Green Burrito"/> <DriveIn Name="KFC"/>
4
13639
by: Jason Gleason | last post by:
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 CharacterCounter(String text,String Character) { int count = 0;
0
251
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 "cow", "over", "moon" that would pass as all
13
2003
by: athiane | last post by:
I want a way to parse out all function names that appear in a couple of C files. When the parsing logic finds a function name in a file, it should print out the Function name, line number and file in which the Function was found. What approach should i follow to tackle this problem ? Is there any option in the gcc compiler that prints out this information ?
4
3375
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 and searching it, but would rather focus on a performance-minded solution. Any sugesstions for a...
8
5646
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 buffer with the bit structure, 4) shifting the char buffer one left, 5) repeat at step 3)) but I'm...
275
12288
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
8613
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9172
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8880
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6532
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
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
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.