Enhance J2ME String Ability - Segment string (attached code)

xiaoxiao2021-03-06  42

Enhance J2ME String Ability - Segment string (attached code)

Author: Chen Yuefeng

From: http://blog.9cbs.neet/mailbomb

After JDK1.4, the STRING class has added a SPLIT method to implement the split of the string, but there is no such method in J2ME (there is no implementation in MIDP2.0), but in the actual use process, sometimes Using this operation, here I have shared a piece of code that I have implemented, everyone shared, and everyone's more comments and suggestions:

/ **

* Segment string, principle: Detect split strings in the string, then tap

* @Param Original needs split strings

* @Paran Regex split string

* @returN-generated array of strings after segmentation

* /

Private static string [] split (string Original, String Regex)

{

// Take the start position of the skewers

INT StartIndex = 0;

// Put the result data first into the vector

Vector v = new vector ();

// Return the result string array

String [] str = null;

// Start position when the string is string

INT INDEX = 0;

// Get positions of matching substrings

StartIndex = Original.Indexof (Regex);

//System.out.println ("0 " startIndex);

// If the position of the start string is smaller than the length of the string, it is proved that the end of the string is not taken.

// - 1 represents the end of the end

While (StartIndex

{

String Temp = Original.Substring (INDEX, STARTINDEX);

System.out.println ("" startIndex);

// Take a skewer

v.addeElement (TEMP);

// Set the starting position of tap strings

INDEX = StartIndex Regex.Length ();

// Get positions of matching substrings

StartIndex = Original.indexof (Regex, StartIndex Regex.Length ());

}

// Take the end of the substring

v.addelement (ORIGINAL.SUBSTRING (INDEX 1 - Regex.length ()));

// convert the Vector object into an array

Str = new string [v.size ()];

For (int I = 0; i

{

STR [I] = (string) v.Elementat (i);

}

// Return to the generated array

Return Str;

}

转载请注明原文地址:https://www.9cbs.com/read-53751.html

New Post(0)