I'm using split to separate strings by space, enter, and semicolon, but when it's a semicolon I need to store it in a position in the array as well. How to do this?
Current Code:
String[] textoSeparado = texto.split(" |\n|;");
I'm using split to separate strings by space, enter, and semicolon, but when it's a semicolon I need to store it in a position in the array as well. How to do this?
Current Code:
String[] textoSeparado = texto.split(" |\n|;");
After reading a few times, I think I understand what you're looking for:
Separate a string using space (""), line break (\ n or \ r) and semicolon (;) as delimiters, but including
;
as a list item.
If it is correct, I was able to combine a regex that meets these conditions, and the result was:
String[] stringSeparada = teste.split("((?<=;)|(?=;))|\s");
See the test below:
//dois exemplos com ambos os tipos de quebras de linhas(windows/linux)
String teste = ";espaco pontoevirgula;QUEBRA\ndelinha;";
String teste2 = ";espaco pontoevirgula;QUEBRA\rdelinha;";
String regex = "((?<=;)|(?=;))|\s";
String[] stringSeparada = teste.split(regex);
String[] stringSeparada2 = teste2.split(regex);
System.out.println(Arrays.toString(stringSeparada));
System.out.println(Arrays.toString(stringSeparada2));
Output:
[;, space, dot-line,;, BREAK, deline,;]
[;, space, point, point,;, BREAK, deline,;]
Note that regex will capture ;
regardless of where it appears.
See working online at IDEONE .