Using system. collections;
// Input: string to be separated, Separator
// Output: String Array
Public static string [] Split (string strinput, string SP)
{
String TMP = "";
Int strlen = 0, splen = 0;
Int found = 0;
String [] Rt = NULL; // returns an array
Try
{
// Input judgment
If (strinput = NULL | sp = NULL | strinput. Length = 0 | sp. Length = 0) return NULL;
// Initialize an array List (as a dynamic array)
Arraylist tmp3 = new arraylist ();
Strlen = strinput. length; // obtain the length of the input string
Splen = sp. length; // delimiter Length
For (INT I = 0; I <strlen; I ++)
{
// Search for Separators
Found = strinput. indexof (SP, I );
If (found> = 0)
{
TMP = "";
// Take the string before the Separator
TMP = strinput. substring (I, found-I );
// Add to array list
Tmp3.add (TMP );
I = found + splen-1;
}
Else
{
String tmp2 = "";
// Obtain the final string
Tmp2 = strinput. substring (I );
If (tmp2! = "")
Tmp3.add (tmp2 );
Break;
}
}
// Set the dimension of the dynamic array to the number of actually existing elements, because the array list is incremented by a multiple of 16.
Tmp3.trimtosize ();
// Convert the array list to a string array and return it.
RT = (string []) tmp3.toarray (typeof (string ));
Tmp3.clear ();
}
Catch (exception E)
{
// Console. writeline ("{0}", E. Message );
Throw E;
}
Return RT;
}
Http://www.gwuxiu.com