Solved

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

Posted on 2014-01-21
3
289 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
ID: 39797744
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
ID: 39797781
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
ID: 39801910
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

867 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

17 Experts available now in Live!

Get 1:1 Help Now