Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-01-21
3
Medium Priority
?
298 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

604 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