using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace TestCount { class Program { static void Main(string[] args) { string targetString = "cccbeebbb"; StringBuilder sb = new StringBuilder(); List<string> resultList = new List<string>(); for (int i = 0; i < targetString.Length; i++) { var result = from target in targetString where target == targetString[i] select target; if (result.Count() > 1) { if (!resultList.Contains(targetString[i].ToString())) { resultList.Add(targetString[i].ToString()); resultList.Add(result.Count().ToString()); } } } foreach (string item in resultList) { sb.Append(item); } Console.WriteLine(sb.ToString()); Console.ReadLine(); } } } 哈哈用LINQ的方法不过貌似不能达到你相同字母的计数还分开来的效果,明天在想下回复你哈哈~
(?is)([a-z])\1*匹配一下就可以了,长度就是每个匹配结果字符串的长度
都搞这个复杂?string result = Regex.Replace("cccbdddeee", @"(\w)\1*", delegate(Match m) { return m.Groups[1].Value + m.Groups[0].Length });
{
static void Main(string[] args)
{
string test = "aaa,中国,人民,bbb,中国,服务,中国,aaa,ccc";
foreach (StringTimes st in GetStringAndTimes(test))
{
Console.WriteLine(st.timers + "," + st.value);
} //foreach (StringTimes st in OrderByTimers(GetStringAndTimes(test)))
//{
// Console.WriteLine(st.value + "-出现次数" + st.timers.ToString() + ";");
//} Console.WriteLine("根据出现次数排序后:");
foreach (StringTimes st in OrderByTimers(GetStringAndTimes(test)))
{
Console.Write(st.value + " ");
}
Console.ReadKey();
} //对StringTimes进行排序
static List<StringTimes> OrderByTimers(List<StringTimes> ST)
{
List<StringTimes> st = ST;
for (int i = 0; i < st.Count; i++)
{
int currentTimes = st[i].timers;
for (int j = i; j < st.Count; j++)
{
if (st[j].timers > currentTimes)
{
StringTimes tempInt = st[i];
st[i] = st[j];
st[j] = tempInt;
currentTimes = st[j].timers;
}
else
{
continue;
}
}
} return st;
} static List<StringTimes> GetStringAndTimes(string Str)
{
List<StringTimes> timesAndString = new List<StringTimes>(); string[] strArray = Str.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (string str in strArray)
{
bool hasShow = false;
foreach (StringTimes st in timesAndString)
{
if (str == st.value)
{
st.timers++;
hasShow = true;
break;
}
else
{
continue;
}
}
if (!hasShow)
timesAndString.Add(new StringTimes(1, str));
} return timesAndString;
} class StringTimes
{
public int timers;
public string value; public StringTimes(int Timers, string Value)
{
this.timers = Timers;
this.value = Value;
}
} }
{
string str = "cccbeebbb"; char[] charStr = str.ToCharArray(); string afterStr = ""; int count = 1; for (int i = 0; i < charStr.Length; i++)
{
if (i == charStr.Length - 1)
{
afterStr += charStr[i] + count.ToString();
}
else
{
if (charStr[i] != charStr[i + 1])
{
afterStr += charStr[i] + count.ToString();
count = 1;
}
else
{
count++;
}
}
} Console.WriteLine(afterStr);
Console.ReadKey();
}
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace TestCount
{
class Program
{
static void Main(string[] args)
{
string targetString = "cccbeebbb";
StringBuilder sb = new StringBuilder();
List<string> resultList = new List<string>();
for (int i = 0; i < targetString.Length; i++)
{
var result = from target in targetString
where target == targetString[i]
select target;
if (result.Count() > 1)
{
if (!resultList.Contains(targetString[i].ToString()))
{
resultList.Add(targetString[i].ToString());
resultList.Add(result.Count().ToString());
}
}
}
foreach (string item in resultList)
{
sb.Append(item);
}
Console.WriteLine(sb.ToString());
Console.ReadLine();
}
}
}
哈哈用LINQ的方法不过貌似不能达到你相同字母的计数还分开来的效果,明天在想下回复你哈哈~
{
return m.Groups[1].Value + m.Groups[0].Length
});