Reputation: 16410
I have to tokenize the following String
12/12/2010:{content1:[{xyz,abc}],13/12/2010:{content2:[{xyz,abc},{content3:[{aa,bb}]}]
I nee to split up the above string if it has }] consequtively. So I did,
String[] tokens = null;
StringTokenizer csvToken = new StringTokenizer(csvString,"]}");
tokens = new String[csvToken.countTokens()];
int tmp = 0;
while(csvToken.hasMoreTokens()) {
tokens[tmp++] = csvToken.nextToken();
}
But it is not tokenizing as I expected.
12/12/2010:{content1:[{xyz,abc
,13/12/2010:{content2:[{xyz,abc
,{content3:[{aa,bb
But What I expect was,
12/12/2010:{content1:[{xyz,abc
,13/12/2010:{content2:[{xyz,abc},{content3:[{aa,bb
how Could I make the code to work as expected?
Upvotes: 0
Views: 2786
Reputation: 142
what about String.split(String regex)?
String toDo = "12/12/2010:{content1:[{xyz,abc}],13/12/2010:{content2:[{xyz,abc},{content3:[{aa,bb}]}]";
String[] splitted = toDo.split("\\}\\]");
for (String s : splitted) {
System.out.println(s);
}
Upvotes: 3
Reputation: 89169
Ok, seeing that there's no answers yet, my "quick fix" are as follows:
import java.util.StringTokenizer;
public class Test {
public static void main(String[] args) {
String csvString = "12/12/2010:{content1:[{xyz,abc}],13/12/2010:{content2:[{xyz,abc},{content3:[{aa,bb}]}]";
String[] tokens = null;
StringTokenizer csvToken = new StringTokenizer(csvString,"]}");
tokens = new String[csvToken.countTokens() - 1];
int tmp = 0;
while(csvToken.hasMoreTokens()) {
tokens[tmp++] = csvToken.nextToken();
if (tmp == tokens.length) {
tokens[tmp - 1] += csvToken.nextToken();
}
}
for (String token : tokens) {
System.out.println(token);
}
}
}
Personally, I'll use String.split()
method and use my solution "idea" as above. Or, if you're brave, use regular expressions.
PS Code tested and worked....
Output:
12/12/2010:{content1:[{xyz,abc
,13/12/2010:{content2:[{xyz,abc,{content3:[{aa,bb
Upvotes: 1