Problem extracting items from a string with separators
Hey guys!
I wanted to know if you could help me out here. You could say I'm new to java, I haven't used it for some time, so this question is pretty basic, but it's driving me mad.
I have a String with the format: 'John','Mary','Anne'
It has a variable length, it could have 3 names on it, 10 or just 1.
I just want to extract the names, and save them elsewhere, for example in a String array.
I've tried this:
Code:
int index = 0;
int prev = 0;
int i = 0;
String [] frag = null;
do {
prev = index;
index = text.indexOf("','", ant);
if (index != -1) {
frag [i]= new String(text.substring(prev + 3, index));
i++;
}
} while (index != -1);
//still have to add the last element
prev = index;
index = text.indexOf("'", prev);
frag [i] = new String(text.substring(prev + 3, index));
But I'm getting an index out of bounds exception, due to the "prev+3" I used.
Please help! I realize it's a very simple piece of code, that's why it's making me mad I can't get around to code such a simple problem!
thanks in advance
PS: can't use StringTokenizer, I'm programming this in j2me. But I posted here because I think it's a basic java question, it has nothing to do with mobile devices or the j2me characteristics.