自己来实现一下。
![ContractedBlock.gif](https://img.php1.cn/3cd4a/18ace/696/1d8e759bd3e6bbec.jpeg)
![ExpandedBlockStart.gif](https://img.php1.cn/3cd4a/189d8/b64/5b34b53b79a39fdd.jpeg)
public static List<string> Splite(string src,char patChar)
{
List<string> result &#61; new List<string>();
if (string.IsNullOrEmpty(src))
{
return null;
}
else
{
int endIndex &#61; 0;
int frontIndex &#61; -1;
while (endIndex<src.Length)
{
if (src[endIndex] !&#61; patChar)
{
}
else
{
string temp &#61; string.Empty;
if (endIndex - frontIndex - 1 > 0)
{
temp &#61; src.Substring(frontIndex &#43; 1, endIndex - frontIndex-1);
}
result.Add(temp);
frontIndex &#61; endIndex;
}
endIndex&#43;&#43;;
}
if (frontIndex <&#61;src.Length-1)
{
string temp&#61;string.Empty;
if (frontIndex &#43; 1 <&#61; src.Length - 1)
{
temp &#61; src.Substring(frontIndex &#43; 1);
}
result.Add(temp);
}
return result;
}
}
public static string[] SpliteArray(string src, char patChar)
{
List<string> re&#61; Splite(src,patChar);
if (re !&#61; null)
{
return re.ToArray();
}
else
{
return null;
}
}
单元测试如下:
![ContractedBlock.gif](https://img.php1.cn/3cd4a/18ace/696/1d8e759bd3e6bbec.jpeg)
![ExpandedBlockStart.gif](https://img.php1.cn/3cd4a/189d8/b64/5b34b53b79a39fdd.jpeg)
///
///Splite 的测试
///
[TestMethod()]
public void SpliteTest()
{
string[] srcList &#61;
{
"i love this game",
" i love this game",
" i love this game",
"i love this game ",
"i love this game ",
"i love this game",
"i love this game ",
" i love this game ",
" i love this game ",
"i love this game",
" i love this game ",
};
foreach (string src in srcList)
{
string[] expected &#61; src.Split(&#39; &#39;);
string[] actual;
actual &#61; StringMagic.SpliteArray(src, &#39; &#39;);
bool equal &#61; true;
for (int i &#61; 0; i < expected.Length; i&#43;&#43;)
{
if (expected[i] !&#61; actual[i])
{
equal &#61; false;
break;
}
}
Assert.AreEqual(equal, true);
}
}