Im пытается заполнить ArrayList словами, но иногда он добавляет пустой символ, почему? Как я могу избежать этого?Разбиение строк с регулярным выражением. Java
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));
}
Результат:
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
'' [\\ s \\? \\.,:; \\) \\ (] + "'? или '" \\ W + "'? –
Держу пари, что метод split вводит emptyStrings, где два разделителя смежны друг с другом (а также если они совпадают в начале и в конце) – Martin