自己来实现一下。


public static List<string> Splite(string src,char patChar)
{
List<string> result = new List<string>();
if (string.IsNullOrEmpty(src))
{
return null;
}
else
{
int endIndex = 0;
int frontIndex = -1;
while (endIndex<src.Length)
{
if (src[endIndex] != patChar)
{
}
else
{
string temp = string.Empty;
if (endIndex - frontIndex - 1 > 0)
{
temp = src.Substring(frontIndex + 1, endIndex - frontIndex-1);
}
result.Add(temp);
frontIndex = endIndex;
}
endIndex++;
}
if (frontIndex <=src.Length-1)
{
string temp=string.Empty;
if (frontIndex + 1 <= src.Length - 1)
{
temp = src.Substring(frontIndex + 1);
}
result.Add(temp);
}
return result;
}
}
public static string[] SpliteArray(string src, char patChar)
{
List<string> re= Splite(src,patChar);
if (re != null)
{
return re.ToArray();
}
else
{
return null;
}
}
单元测试如下:


/// <summary>
///Splite 的测试
///</summary>
[TestMethod()]
public void SpliteTest()
{
string[] srcList =
{
"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 = src.Split(' ');
string[] actual;
actual = StringMagic.SpliteArray(src, ' ');
bool equal = true;
for (int i = 0; i < expected.Length; i++)
{
if (expected[i] != actual[i])
{
equal = false;
break;
}
}
Assert.AreEqual(equal, true);
}
}