Enhance the string capability of j2m‑split string (with source code)

Source: Internet
Author: User

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;

}

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.