C # implement the delimiter split Function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.