Reputation: 741
public static void main(String[] args){
String s=new String("f;sjdkfd:fdsfdf;df:FD::D:::F:RE:FRER:F:ER:FR:F:RF:GR:G:RG: :P");
String[] ss = s.split(":");
for(String token : ss){
System.out.println(token);
}
}
using this code, I can get the token between ":". But I wan to split the text with both ":"and";" at the same time. is that possible?
Upvotes: 3
Views: 741
Reputation: 686
You can use a regular expression:
String[] ss = s.split("[:;]")
So, the code will be:
public static void main(String[] args) {
String s = new String("f;sjdkfd:fdsfdf;df:FD::D:::F:RE:FRER:F:ER:FR:F:RF:GR:G:RG: :P");
String[] ss = s.split("[:;]");
for (String token : ss) {
System.out.println(token);
}
}
Upvotes: 3
Reputation: 34657
Use Commons Lang, specifically [this StrTokenizer constructor](http://commons.apache.org/lang/api-3.1/org/apache/commons/lang3/text/StrTokenizer.html#StrTokenizer(char[], char)):
StrTokenizer tokenizer = new StrTokenizer("f;sjdkfd:fdsfdf;df:FD::D:::F:RE:FRER:F:ER:FR:F:RF:GR:G:RG: :P".toCharArray(), ":");
tokenizer.next(); // will give you "f;sjdkfd", followed by fdsfdf;df, etc. with every call to .next()
Upvotes: 1
Reputation: 106385
You could use character class instead:
String[] ss = s.split("[:;]");
Upvotes: 2