Solved

List  - quickest way to count the number of "List keywords" in a string  (c#)

Posted on 2014-01-21
3
287 Views
Last Modified: 2014-01-23
Imagine I have a list with  these keywords "pencil; ink; paper; rubber; stylus,...." .

I need to know how many keywords are in the string (text). It does not matter which keywords are!  In the following text

"To work I need paper and pencil. And of course a rubber because I make many mistakes"

the result will be 3

Thks in advance.
0
Comment
Question by:novreisb
3 Comments
 
LVL 15

Expert Comment

by:ZabagaR
Comment Utility
Can you use this person's code:

http://r3dux.org/2012/10/how-to-count-word-occurences-in-a-string-or-file-using-csharp/

The only thing he does differently is that the source he's parsing is a text file, whereas you just wrote the line "To work I need paper and pencil. And of course a rubber because I make many mistakes"

If you're reading your line of text-to-be-searched from a file, then you can flat out use that code from this web site.
0
 

Author Comment

by:novreisb
Comment Utility
I was trying do the Count without a loop ("more efficient code"), but if it is not possible I will use one I have already in my code.

Thks anyway
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
Comment Utility
If you set up Lists of strings first then you can count with short code:
            string wrds = "pencil; ink; paper; rubber; stylus";
            string data = "To work I need paper and pencil. And of course a rubber because I make many mistakes";

            List<string> wrds2 = wrds.Split(new char[] { ';', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList<string>();
            List<string> data2 = data.Split(new char[] { ';', ' ', '.' }, StringSplitOptions.RemoveEmptyEntries).ToList<string>();

            int count = data2.Count(p => wrds2.Contains(p));

Open in new window

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now