Solved

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

Posted on 2014-01-21
3
292 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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