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. 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.
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.
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.
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.
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());
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());
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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'
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |