473,387 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1587
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.Diagnostics.Debug.WriteLine("found: " + needle);

source = source.Remove(pos, needle.Length);
}
}

--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072
"Spondishy" <sp*******@tiscali.co.uk> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.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.Diagnostics.Debug.WriteLine("found: " + needle);

source = source.Remove(pos, needle.Length);
}
}

--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072
"Spondishy" <sp*******@tiscali.co.uk> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.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(string[] testForStrings, string testString)
{
System.Collections.Hashtable testHash = new Hashtable();
foreach(string item in testForStrings)
{
bool isThere = (testString.IndexOf(item) > -1);
testHash.Add(item, isThere);
if(isThere)
{
testString = testString.Remove(testString.IndexOf(item),
item.Length);
}
}
return !testHash.ContainsValue(false);
}

Then I basically, to test, did the following:

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

MessageBox.Show(TestString(items, 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(string[] testForStrings, string testString)
{
System.Collections.Hashtable testHash = new Hashtable();
foreach(string item in testForStrings)
{
bool isThere = (testString.IndexOf(item) > -1);
testHash.Add(item, isThere);
if(isThere)
{
testString = testString.Remove(testString.IndexOf(item),
item.Length);
}
}
return !testHash.ContainsValue(false);
}

Then I basically, to test, did the following:

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

MessageBox.Show(TestString(items, 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*******@tiscali.co.uk> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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*******@tiscali.co.uk> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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
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...
4
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...
3
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...
4
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...
0
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...
13
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...
4
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...
8
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...
275
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...

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.