I have String called "RamaSubbaReddyabcdaacacakkkoooahgafffgahgghsa". I want to know number of "a" characters available in the give String. As per my knowledge I found two ways to find the count. That are: 1) By using String.Split() 2) Linq Lambda Expression
My Observations:
1) If i use String.Split() it is returning wrong result 2) If i use Linq Lambda Expression it is returning correct result.
Here my doubt is how can i get the count of the given split character from the given string by using String.Split()
And also please suggest me which is the best way to get count of the given split character from the given string either "String.Split()" or "Linq Lambda" expression?
Please find the complete example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SplitData
{
class Program
{
static void Main(string[] args)
{
SplitData("RamaSubbaReddyabcdaacacakkkoooahgafffgahgghsa", 'a');
SplitData("RamaSubbaReddyabcdaacacakkkoooahgafffgahgghsa", 'r');
SplitData("RamaSubbaReddyabcdaacacakkkoooahgafffgahgghsa", 'R');
SplitData("RamaSubbaReddyabcdaacacakkkoooahgafffgahgghsa", 'm');
SplitData("RamaSubbaReddyabcdaacacakkkoooahgafffgahgghsa", 'd');
SplitData("RamaSubbaReddyabcdaacacakkkoooahgafffgahgghsa", 'g');
SplitData("RamaSubbaReddyabcdaacacakkkoooahgafffgahgghsa", 's');
SplitData("RamaSubbaReddyabcdaacacakkkoooahgafffgahgghsa", 'o');
SplitData("RamaSubbaReddyabcdaacacakkkoooahgafffgahgghsa", 'c');
SplitData("RamaSubbaReddyabcdaacacakkkoooahgafffgahgghsa", 'u');
SplitData("RamaSubbaReddyabcdaacacakkkoooahgafffgahgghsa", 'f');
Console.ReadKey();
}
private static void SplitData(string data,char split)
{
// using lambda expresion
int len = data.AsEnumerable().Where(x => x.ToString().ToLower().Contains(split)).Count();
Console.WriteLine("Total '" + split + "' available are:{0} using lambda", len.ToString());
//using normal split function
len = data.Split(split).Length;
Console.WriteLine("Total '" + split + "' available are:{0} using normal split", len.ToString());
}
}
}