public string[] Split(string input, int limit)
{
int index = 0;
bool matchLimited = limit > 0;
List <string> matchList = new List <string>();
Matcher m = Matcher(input);
// Add segments before each match found
while (m.Find())
{
if (!matchLimited || matchList.Count < limit - 1)
{
String match = input.Substring(index, m.Start()).ToString();
matchList.Add(match);
index = m.End();
}
else if (matchList.Count == limit - 1)
{ // last one
String match = input.Substring(index,
input.Length).ToString();
matchList.Add(match);
index = m.End();
}
}
// If no match was found, return this
if (index == 0)
{
return new String[] { input.ToString() }
}
;
// Add remaining segment
if (!matchLimited || matchList.Count < limit)
{
matchList.Add(input.Substring(index, input.Length).ToString());
}
// Construct result
int resultSize = matchList.Count;
if (limit == 0)
{
while (resultSize > 0 && matchList[resultSize - 1].Equals(""))
{
resultSize--;
}
}
return(matchList.SubList(0, resultSize).ToArray());
}
}