110,533
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 获取指定字符串的个数
/// </summary>
/// <param name="src">源字符串</param>
/// <param name="arg">指定字符串,格式i;i,如1;2</param>
/// <returns></returns>
private int getCount(string src, string arg)
{
Regex reg = new Regex(@"\b" + Regex.Escape(arg) + @"\b");
return reg.Matches(src).Count;
}
string str = "1;1|1;8|1;10|2;1|3;7|4;9|1;3|2;1|";
var query = str.Split('|').Where(w => w.StartsWith("1") && w.Length > 0)
.GroupBy(g => g)
.Select(s => new { key = s.Key, count = s.Count() });
foreach循环出来
static void Main(string[] args)
{
string s = "1;1|1;8|1;10|2;1|3;7|4;9|1;3|2;1|";
List<int> l = new List<int>();
int iTmp;
foreach (var item in from m in s.Split('|')
where m.StartsWith("1;") && m.Length > 2
select m.Substring(2))
{
if (!int.TryParse(item, out iTmp))
{
continue;
}
while (l.Count <= iTmp)
{
l.Add(0);
}
l[iTmp]++;
}
for (int i = 0; i < l.Count; i++)
{
if (l[i]>0)
{
Console.WriteLine("1;" + i + ":" + l[i] + "个");
}
}
}
void Main()
{
string str="1;1|1;8|1;10|2;1|3;7|4;9|1;3|2;1|1;1";
string temp="1;1"; //自己定义
int count=str.Split('|').Count(s=>s==temp);
Console.WriteLine(count); //2
}