hi friends,
please how to find the count of string variable
for example in a line i am consists word 'this' four times.
if i sear for this word then it should 4,
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Jul 10, 2014, 12:27 PM EDT
The output is:
The word 'this' occurs 4 time(s)
Note that case is ignored here so 'This' and 'this' are both counted.
Abhay ShankerRank #375 · 6.7K reputationPosted Jul 10, 2014, 8:52 PM EDT
using System;
using System.Linq;
namespace StringCount
{
class Program
{
static void Main(string[] args)
{
string text = "This is a pen.This is a cat.This is a room.This is an elephant.You say this ";
string searchTerm = "this";
string[] source = text.Split(new char[] { '.' ,' ' }, StringSplitOptions.RemoveEmptyEntries);
var matchQuery = from word in source
where word.ToLowerInvariant() == searchTerm.ToLowerInvariant()
select word;
// Count the matches, which executes the query.
int wordCount = matchQuery.Count();
Console.WriteLine("{0} occurrences(s) of the search term \"{1}\" were found.", wordCount, searchTerm);
// Keep console window open in debug mode
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
}
}