I am working on android. I have a string like below.
String str = "@test1|test2|test3@test4|test5|test6|test7@test8|test9|test10|test11@test12|test13|test14|test15@......"
I am splitting the above string with "@" using String[] str1 = str.split("[@]"); so that the final results are
str1[0] = test1|test2|test3
str1[1] = test4|test5|test6|test7
...
The code snippet I used
for (int x = 1; x < str1.length; x++) { String[] st1 = str1 [x].trim().split("\\|"); System.out.println("first" + st1[0].trim()); System.out.println("second" + st1[1].trim()); System.out.println("third" + st1[2].trim()); System.out.println("fourth" + st1[3].trim()); List1.add(st1[0].trim()); List2.add(st1[1].trim()); List3.add(st1[2].trim()); List4.add(st1[3].trim()); } In the above for loop, when the loop starts from x=2 it is working fine. But if I give x=1 then it is throwing array index out of bounds exception at "System.out.println("fourth" + st1[3].trim());". Is because str1[0] consists of only 3 items whereas the remaining consists of 4 items. So now I am unable to get the fourth value after splitting with "|". Please tell me how to get the fourth value. Thanks in advance.