Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中使用string.split()方法或Linq Lambda表达式从给定字符串中查找拆分字符长度的最佳方法是什么_C#_String_Linq_Lambda_Split - Fatal编程技术网

C# 在C中使用string.split()方法或Linq Lambda表达式从给定字符串中查找拆分字符长度的最佳方法是什么

C# 在C中使用string.split()方法或Linq Lambda表达式从给定字符串中查找拆分字符长度的最佳方法是什么,c#,string,linq,lambda,split,C#,String,Linq,Lambda,Split,我有一个叫做Ramasubaredyabcdaacacakkooahgaffgahgghsa的字符串。我想知道给定字符串中可用的字符数。据我所知,我找到了两种方法来找到伯爵。它们是:1通过使用String.Split 2 Linq Lambda表达式 我的意见: 1如果使用String.Split,则返回错误的结果 2如果我使用Linq Lambda表达式,它将返回正确的结果 这里我的疑问是,如何使用string.split从给定字符串中获取给定拆分字符的计数 另外,请告诉我,从给定字符串中获取

我有一个叫做Ramasubaredyabcdaacacakkooahgaffgahgghsa的字符串。我想知道给定字符串中可用的字符数。据我所知,我找到了两种方法来找到伯爵。它们是:1通过使用String.Split 2 Linq Lambda表达式

我的意见: 1如果使用String.Split,则返回错误的结果 2如果我使用Linq Lambda表达式,它将返回正确的结果

这里我的疑问是,如何使用string.split从给定字符串中获取给定拆分字符的计数

另外,请告诉我,从给定字符串中获取给定拆分字符计数的最佳方法是string.split还是Linq Lambda表达式

请找到完整的示例:
你是说你想数一封信的发生次数?像这样

String data = "RamaSubbaReddyabcdaacacakkkoooahgafffgahgghsa";
Char letter = 'a';
Int32 totalOccurances = data.Count(character => character == letter);
如果需要不区分大小写的计数,则:

string str = "RamaSubbaReddyabcdaacacakkkoooahgafffgahgghsa";
char searchChar = 'a';
int countA = str.Count(r => char.ToUpperInvariant(r) == char.ToUpperInvariant(searchChar));
若您要求在string.Split和Linq Count之间选择最佳选项,那个么在我看来,Linq更具可读性。我不确定性能,但我怀疑LINQ版本会更快

如果要使用string.Split并使其不区分大小写,请构造一个包含两个元素的字符数组,使用大写和小写,然后使用Split,如:


虽然不是一个奇特的LINQ解决方案,但

int count = CountChar("RamaSubbaReddyabcdaacacakkkoooahgafffgahgghsa", 'a');
.....

int CountChar(string input, char toFind)
{
    int count = 0;
    int pos = -1;
    while((pos = input.IndexOf(toFind, pos+1)) != -1)
        count++;
    return count;
}
从职位开始 还有

编辑:现在我很好奇,决定用这个和lambda解决方案来测量时间。 差别是显著的

void Main()
{
    string str = "RamaSubbaReddyabcdaacacakkkoooahgafffgahgghsa";
    Stopwatch sw = new Stopwatch();
    sw.Start();
    for(int i = 0; i < 10000000; i++)
    {
        int count = CountChar(str, 'a');
     }   
    sw.Stop();
    Console.WriteLine("Using IndexOf:" + sw.ElapsedMilliseconds.ToString());  
    sw.Reset();
    sw.Start();
    for(int i = 0; i < 10000000; i++)
    {
        int countA = str.Count(r => r == 'a');
    }
    sw.Stop();
    Console.WriteLine("Using Count:" + sw.ElapsedMilliseconds.ToString());
}
第一个循环以1160毫秒结束,第二个循环以6200毫秒结束。
是否有人能发现此度量中是否存在问题?

对于不区分大小写的比较,可以使用实例或等效枚举。。至于你想怎么写,选择你的毒药=

// caller specifies comparison type
int Count1(string str, char searchChar, StringComparison comparison = StringComparison.CurrentCultureIgnoreCase)
{
    string searchStr = searchChar.ToString();
    int count = 0;
    for (int i = 0; i < str.Length; i++)
        if (string.Equals(searchStr, str[i].ToString(), comparison))
            count++;
    return count;
}
// ordinal comparison
int Count2(string str, char searchChar)
{
    string searchStr = searchChar.ToString();
    int count = 0;
    for (int i = 0; i < str.Length; i++)
        if (searchChar == str[i])
            count++;
    return count;
}
// ordinal comparison
int Count3(string str, char searchChar)
{
    return str.Split(searchChar).Length - 1;
}
// ordinal comparison
int Count4(string str, char searchChar)
{
    return str.Count(c => c == searchChar);
}
// caller specifies comparison type
int Count5(string str, char searchChar, StringComparison comparison = StringComparison.CurrentCultureIgnoreCase)
{
    string searchStr = searchChar.ToString();
    return str.Count(c => string.Equals(c.ToString(), searchStr, comparison));
}

这两个功能的可能重复项并不相等。第一次你拿了绳子。第二次你拿了绳子。嗨,乔恩。这不是一个重复的问题。请仔细阅读问题,然后再重复提问。。因为我要问的是:>>>>>>>>请在上面的问题中引用它,我的疑问是如何使用string.split从给定字符串中获取给定拆分字符的计数,还请建议我从给定字符串中获取给定拆分字符计数的最佳方法是string.split还是Linq Lambda表达式?@ramasubarreddym:链接的问题地址显示了如何使用String.Split执行此操作,并讨论了哪个选项最好。它是重复的。嗨,哈比卜,我如何使用string.split从给定字符串中获取给定拆分字符的计数?。通过使用linq概念,我已经得到了正确的计数。@ramasubreddym data.Splitsplit.Length-1注意,情况就是这样sensitive@RamaSubbaReddyM,您可以执行var count=str.Splitnew[]{'a','a'}.Length-1;但是你必须构造一个大小写字符数组,使其不区分大小写。我很难理解使用Count和Split,然后计算Split集的大小在结果上的差异。这不会返回完全相同的计数吗?@ramasubaredym,我刚刚更新了我的答案,使用string.Split来计算字符大小写不敏感的出现次数hi Sircidesakit,如何使用string.Split从给定字符串中获取给定拆分字符的计数?。我已经使用linq概念得到了正确的计数。给定拆分字符的计数是多少?示例?是的,我想要一个示例,如何通过使用string获得给定字符串中可用的拆分字符数“a”。拆分“a”这比在字符处拆分字符串并构建字符串数组要好得多。不确定它是否比所提出的LINQ解决方案性能更好,但对于这些字符串来说,这是一个真正的微观差异。实际上,这可能更快,因为每次迭代都没有lambda回调。因此,这将为您节省至少十亿分之一秒的时间。甚至两个!关于你的编辑是的,这正是我所期望的。Recusion/Lambda很有用,因为它们在概念上简化了程序,但它们肯定不会给您带来任何性能优势。他们确实为并行解决方案开辟了道路……天哪,这太彻底了!
int count = CountChar("RamaSubbaReddyabcdaacacakkkoooahgafffgahgghsa", 'a');
.....

int CountChar(string input, char toFind)
{
    int count = 0;
    int pos = -1;
    while((pos = input.IndexOf(toFind, pos+1)) != -1)
        count++;
    return count;
}
void Main()
{
    string str = "RamaSubbaReddyabcdaacacakkkoooahgafffgahgghsa";
    Stopwatch sw = new Stopwatch();
    sw.Start();
    for(int i = 0; i < 10000000; i++)
    {
        int count = CountChar(str, 'a');
     }   
    sw.Stop();
    Console.WriteLine("Using IndexOf:" + sw.ElapsedMilliseconds.ToString());  
    sw.Reset();
    sw.Start();
    for(int i = 0; i < 10000000; i++)
    {
        int countA = str.Count(r => r == 'a');
    }
    sw.Stop();
    Console.WriteLine("Using Count:" + sw.ElapsedMilliseconds.ToString());
}
// caller specifies comparison type
int Count1(string str, char searchChar, StringComparison comparison = StringComparison.CurrentCultureIgnoreCase)
{
    string searchStr = searchChar.ToString();
    int count = 0;
    for (int i = 0; i < str.Length; i++)
        if (string.Equals(searchStr, str[i].ToString(), comparison))
            count++;
    return count;
}
// ordinal comparison
int Count2(string str, char searchChar)
{
    string searchStr = searchChar.ToString();
    int count = 0;
    for (int i = 0; i < str.Length; i++)
        if (searchChar == str[i])
            count++;
    return count;
}
// ordinal comparison
int Count3(string str, char searchChar)
{
    return str.Split(searchChar).Length - 1;
}
// ordinal comparison
int Count4(string str, char searchChar)
{
    return str.Count(c => c == searchChar);
}
// caller specifies comparison type
int Count5(string str, char searchChar, StringComparison comparison = StringComparison.CurrentCultureIgnoreCase)
{
    string searchStr = searchChar.ToString();
    return str.Count(c => string.Equals(c.ToString(), searchStr, comparison));
}