Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.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# - Fatal编程技术网

C# 正在计算字符串[]中的子字符串出现次数

C# 正在计算字符串[]中的子字符串出现次数,c#,C#,给定如下所示的字符串数组: string[]={"bmw"," ","1bmw"," "}; 我需要计算子字符串bmw在该数组中出现的频率。在本例中,它发生2次 我怎么用C写呢 我想忽略大写字母 sting[]={“宝马”、“宝马”、“1bmw”} 那么计数结果是3 我该怎么办 # 谢谢大家的回答。没有LINQ int counter = 0; foreach(string item in string_array) { if(item.IndexOf("bmw") > -1){

给定如下所示的字符串数组:

string[]={"bmw"," ","1bmw"," "};
我需要计算子字符串
bmw
在该数组中出现的频率。在本例中,它发生2次

我怎么用C写呢

我想忽略大写字母

sting[]={“宝马”、“宝马”、“1bmw”}

那么计数结果是3

我该怎么办

# 谢谢大家的回答。

没有LINQ

int counter = 0;

foreach(string item in string_array)
{
   if(item.IndexOf("bmw") > -1){
     counter++;

   }
}
如果您使用的是C#3,则可以使用LINQ:

var strings = {"bmw"," ","1bmw"," "};
var count = string.Where( s => s.IndexOf( "bmw" ) > -1 ).Count();
否则,循环也会起作用:

string[] strings = {"bmw"," ","1bmw"," "};  
int count = 0;
foreach( string s in strings ) {
    if( s.IndexOf( "bmw" > -1 ) ) {
       count++;
    }
}
尝试:

下一段代码将返回不区分大小写的出现次数(正如作者在评论中提到的):


可以找到有关字符串比较的更多信息。

您可以使用Linq进行如下操作:

var s = new string[] {"bmw", "1bmw"};
var result = s.Where(o=>o.Contains("bmw")).Count();
一种使用LINQ的方法

static int CountOfString(IEnumerable<string> items, string substring, bool isCaseSensitive)
{
    StringComparison comparison = StringComparison.InvariantCulture;
    if (!isCaseSensitive)
        comparison = StringComparison.InvariantCultureIgnoreCase;

    return items.Count(item => item.IndexOf(substring, comparison) > -1);
}

不区分大小写会让它更加冗长,但使用以下内容仍然非常紧凑:

var str = new sting[]={"bmw", "", "BMw","1bmw"};
var count = str.Count(s => s.IndexOf("bmw", StringComparison.InvariantCultureIgnoreCase) > -1);

嗨,sting[]={“bmw”,“bmw”,“1bmw”}有一个“bmw”与“bmw”不完全相同,我也想把这个词算进去。@shaon fan最简单的处理方法是在每个字符串上使用ToLower()进行搜索。嗨,sting[]={“bmw”,“bmw”,“1bmw”}有一个“bmw”与“bmw”不完全相同我还想把这个词算进去。@shaon fan:如果你想进行不区分大小写的比较,只需使用:
IndexOf(“bmw”,StringComparison.OrdinalIgnoreCase)
-OrdinalIgnoreCase参数将匹配比较中的任何大小写。嗨,sting[]={“bmw”,“bmw”,“1bmw”}有一个“bmw”与“bmw”不完全相同我还想把这个词算进去。嗨,sting怎么样?{“bmw”,“bmw”,“1bmw”}有一个“bmw”和“bmw”不完全一样,我也想把这个词算进去。
static int CountOfString(IEnumerable<string> items, string substring, bool isCaseSensitive)
{
    StringComparison comparison = StringComparison.InvariantCulture;
    if (!isCaseSensitive)
        comparison = StringComparison.InvariantCultureIgnoreCase;

    return items.Count(item => item.IndexOf(substring, comparison) > -1);
}
string[] items = { "bmw", " ", "1bmw", " " };
int count = CountOfString(items, "bmw", false);
var str = new sting[]={"bmw", "", "BMw","1bmw"};
var count = str.Count(s => s.IndexOf("bmw", StringComparison.InvariantCultureIgnoreCase) > -1);