Enhance the string capability of j2m‑split string (with source code)
Author: Chen yuefeng
From: http://blog.csdn.net/mailbomb
After jdk1.4, the split method is added to the string class to implement string segmentation, but this method is not available in the j2_m2.0. However, in actual use, in some cases, we do need to use this operation. Here we share some of the code I previously implemented with you. If you have any shortcomings, I would like to give you more comments and suggestions:
/**
* String splitting principle: checks the string to be split and then obtains the substring.
* @ Param Original: string to be split
* @ Paran RegEx split string
* @ Return: The String Array generated after the split
*/
Private Static string [] Split (string original, string RegEx)
{
// Obtain the starting position of the substring
Int startindex = 0;
// Put the result data into the vector first
Vector v = new vector ();
// Returned result string array
String [] STR = NULL;
// Store the starting position of the substring
Int Index = 0;
// Obtain the position of the matched substring
Startindex = original. indexof (RegEx );
// System. Out. println ("0" + startindex );
// If the position of the starting string is smaller than the length of the string, it indicates that it is not obtained to the end of the string.
//-1 indicates that the end is obtained.
While (startindex <original. Length () & startindex! =-1)
{
String temp = original. substring (index, startindex );
System. Out. println ("" + startindex );
// Obtain the substring
V. addelement (temp );
// Set the starting position of the substring
Index = startindex + RegEx. Length ();
// Obtain the position of the matched substring
Startindex = original. indexof (RegEx, startindex + RegEx. Length ());
}
// Obtain the end substring
V. addelement (original. substring (index + 1-RegEx. Length ()));
// Convert a vector object to an array
STR = new string [v. Size ()];
For (INT I = 0; I <v. Size (); I ++)
{
STR [I] = (string) v. elementat (I );
}
// Return the generated Array
Return STR;
}