Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Sample Text:

"health-care providers are in the midst of a burgeoning health-care paradigm shift. As the population over age 65 increases and the number of patients in long-term care climbs, the management of chronic disease is now a reality that few debate. Emphasis on disease management is shifting to coordination of care for chronic disease; this is an optimistic observation. The demographic realities of an increasing older population are expected to eclipse 90 million in 2060. This will be nearly 22% of the total population (U.S. Census, 2013)."

I need the "health-care" count value of this text in the above paragraph(ie.How many times the word is present)
Posted

Learn to do it yourself: Count Occurrences of a Word in a String[^]
 
Share this answer
 
Comments
BillWoodruff 26-Feb-14 13:01pm    
Voted +5 to counter-act the idiot who down-voted this answer.
Peter Leow 27-Feb-14 4:41am    
Thanks BillWoodruff for your kindness.
King Fisher 27-Feb-14 4:29am    
5++;
Peter Leow 27-Feb-14 4:41am    
Thanks King_Fisher.
King Fisher 27-Feb-14 4:43am    
:)
There are a lot of ways to do this (most of the ways described here: Counting Lines in a String[^] can be adapted to do it).
But, the simplest to read is probably:
C#
int count = Regex.Matches(inputString, "health-care").Count;
And if you aren't using large amounts of data, or doing it often, that's probably the one I'd go for.
 
Share this answer
 
Comments
BillWoodruff 26-Feb-14 13:01pm    
Voted +5 to counter-act the idiot who down-voted this answer.
Matt T Heffron 27-Feb-14 14:01pm    
+5
Matt T Heffron 27-Feb-14 14:08pm    
In the general case:
If it is important to exclude similar "words" that include the test word as a subset (e.g., if the test word was "read" and "readers" must be excluded), add the word boundary elements:
int count = Regex.Matches(inputString, @"\bhealth-care\b").Count;
or
int count = Regex.Matches(inputString, string.Concat(@"\b", testWordStringVariable, @"\b")).Count;
 
Share this answer
 
Comments
BillWoodruff 26-Feb-14 13:02pm    
Voted +5 to counter-act the idiot who down-voted this answer.
george4986 26-Feb-14 22:36pm    
thanks BillWoodruff for ur Effort.Good Luck ;-)
Matt T Heffron 27-Feb-14 14:01pm    
+5
george4986 27-Feb-14 22:44pm    
thanks Matt ;-)
C#
public int CountStringOccurrences(string text, string matchword)
    {
	// Loop through all instances of the string 'text'.
	int count = 0;
	int i = 0;
	while ((i = text.IndexOf(matchword, i)) != -1)
	{
	    i += matchword.Length;
	    count++;
	}
	return count;
    }


where string Text is a paragraph and string matchword is the word you want to search
 
Share this answer
 
v2
Comments
BillWoodruff 26-Feb-14 13:05pm    
The variable 'pattern is never defined; I think you meant to use 'matchword instead of 'pattern.
ravikhoda 26-Feb-14 22:46pm    
yeah typo mistake. thanks for suggestion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900