public static IEnumerable<string> Chunk(string str)
{
for (int j = 0; j < str.Length; j++)
{
for (int i = str.Length; i >0; i--)
{
if (i + j > str.Length)
continue;
yield return str.Substring(j,i);
}
}
}
调用方法:
string sResult="";
int i = 0;
foreach (string s in Chunk("abcdefgh"))
{
i++;
sResult += "," + s;
}
MessageBox.Show("解析成子字符串:" + sResult + "\r\n 一共有:" + i + "个子字符串");
运行结果: