?
Solved

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

Posted on 2014-01-21
3
Medium Priority
?
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

777 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