static void Main(string[] args)
{
string str = "ddsdssdsdsgfgfgggggdfaaafddsadsad";
int resultNum = 0;
// 最多的连续字符的数量
char resultChar = ' ';
// 最多的连续字符
int tempNum = 0;
// 当前连续字符的数量
char tempChar = ' ';
// 当前连续字符
for (int i = 0; i < str.Length; i++)
{
if (tempChar != str[i]) // 字符不同,说明已经不连续
{
if (resultNum < tempNum) // 记录最长的连续字符数量和字符
{
resultChar = tempChar;
resultNum = tempNum;
}
tempChar = str[i];
tempNum = 1;
}
else
{
tempNum++;
}
}
// 输出连续的字符
for (int j = 0; j < resultNum; j++)
{
Console.Write(resultChar);
}
Console.ReadKey();
}