Im trying to fill an ArrayList with words, but sometimes it adds an empty character, why? How can i avoid this?
ArrayList<String> textAL = new ArrayList<String>();
String text = "This.IS(a) text example blah? bl:ah";
String regex = "[\\s\\?\\.,:;\\)\\(]";
String[] splittedText = text.split(regex);
for(int i = 0; i < splittedText.length; i++){
if(splittedText[i] != " "){ //ignore whitespace
textAL.add(splittedText[i]);
}
}
for(int i = 0; i < textAL.size(); i++){
System.out.println("t2(" + i + ") "+ textAL.get(i));
}
Result:
textAL(0) This
textAL(1) IS
textAL(2) a
textAL(3)
textAL(4) text
textAL(5) example
textAL(6) blah
textAL(7)
textAL(8) bl
textAL(9)
textAL(10) ah