what is the possible way to split a String by multiple delimiters? Is StringTokenizer can help me out to achieve this?
String str="list1|10456103|10456102|10456121#list2|10456105|10456122";
String str="list1|10513846#list2|";
String str3="list1#list2|10509855";
String str4="list2|10481812|";
String str5="list1|10396496|";
String str6="list1#list2|";
So now I should be able to extract only the long values :
For Str1 Finallist=[10456103,10456102,10456121,10456105,10456122]
For Str2 Finallist=[10513846]
For Str3 Finallist=[10509855]
For Str4 Finallist=[10481812]
For Str5 Finallist=[10396496]
For Str6 Finallist[]
you can split them using the split
method for String in java, then check if it's numeric or not.
the string is split by having #
or |
or ,
once or more than once.
then the split strings are tested to be numeric or not, as so:
public static void main(String []args){
String str="list1|10456103|10456102|10456121#list2|10456105|10456122";
String arr[] = str.split("[|,#]+");
for(String s: arr){
try{
int num=Integer.parseInt(s);
System.out.println(num + " is a number"); //add to list
}catch(Exception err) {
System.out.println(s + " is not a number"); //don't add to list
}
}
}