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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Abhay ShankerPosted Jul 11, 2014, 12:52 AM
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();
}
}
}
VulpesPosted Jul 10, 2014, 4:27 PM
The output is:
The word 'this' occurs 4 time(s)
Note that case is ignored here so 'This' and 'this' are both counted.