Skip to main content

split string is not supported here what can we do to split a string to an a

1 reply [Last post]
dotclass
Offline
Joined: 2008-05-12

if i have a string consists of labla1***blabla2***blabla3***... and so on
now i wanna split it into array of strings string[] blabla{"blabla1","blabla2","blabla3",....} while the method split is not supported in any wireless toolkit so anybody has any clue of how this can be made (without the substring method)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sfitzjava
Offline
Joined: 2003-06-15

It depends on how closely you want the APIs functionality to match. Having something that is general purpose like a regex engine it a bit costly if all you are needing to do is split it on certain characters. In JavaME resources are at a premium and while OO and code reuse is almost always good, if you are running low on resources a Q&D (quick-n-dirty) solution usually takes care of the problem.

However my friend google says that this link may be of some interest. :)
http://jakarta.apache.org/regexp/apidocs/

in particular is the class RE and as the doc says:
"RE is an efficient, lightweight regular expression evaluator/matcher class. Regular expressions are pattern descriptions which enable sophisticated matching of strings. In addition to being able to match a string against a pattern, you can also extract parts of the match. This is especially useful in text parsing! Details on the syntax of regular expression patterns are given below."

But there is no guarantee that porting this will be simple or easy, nor that there may be non-javame api calls that you will have to work around.

The Q&D solution is make an array of int 's that can hold the maximum number of substring references that you will need. Then loop through the string using indexOf( ch, offset) looking for the value to match upon and save the index, and keep a running count. When done use the running count to generate our string[], then loop through the int's and get the substring(st, int[x]), where st is the start offset = 0 in most cases, and int[x] being the location of the next string start position (remember substring is exclusive to the end position).

That's the fun thing about JavaME, you have to write all your utility classes, and sometimes in multiple ways for size, speed, or threading needs.

Regards,
-Shawn