This is my string
String s = "asadsdas357902||190||RUE RACHELLE||ST|||LES CÈDRES|J7T1J9|QC";
I split it as
String a[] = s.split(s, i);
outputs: i=0
| | 1 9 0 | | R U E R A C H E L L E | | S T | | | L E S C È D R E S | J 7 T 1 J 9 | Q C
First two indexes of array are empty nad then each index has one character.
When i=1, output is the whole original string
asadsdas357902||190||RUE RACHELLE||ST|||LES CÈDRES|J7T1J9|QC
when i=2, output is
||190||RUE RACHELLE||ST|||LES CÈDRES|J7T1J9|QC
first index of array is empty and second contains the substring from first | symbol
when i=3, output is
||190||RUE RACHELLE||ST|||LES CÈDRES|J7T1J9|QC
first two indexes are empty and last index has the same substring as for i=2
when i=4, output is
| |190||RUE RACHELLE||ST|||LES CÈDRES|J7T1J9|QC
first two indexes empty, next contains a pipe and last the rest
when i=5, output is
| | 190||RUE RACHELLE||ST|||LES CÈDRES|J7T1J9|QC
first two empty, next two pipe character and last the remaining.
as the i value increases, the output is
first two indexes empty
next all indexes except last contains one character each
last index contains the remaining string
My questions are
- Why it is not considering the first word before the first pipe symbol?
- Why it is making he first two indexes empty for every value of i except 1?
- The pattern is the same string here, so what is matched here and how the outputs come?
And another thing is if I replace the pipe symbol with any other symbol such as @ or ! or %, the output is
array length is 2 with both indexes has empty strings. this is for i>=2
for i=0
the array length is also 0
for i=1
the array length is 1 containing the whole string.
Is it taking the pipe symbol as a special regex symbol?
Any help appreciable.
splitmethod takes a regex as an input param. Now the regex in your case isasadsdas357902||190||RUE RACHELLE||ST|||LES CÈDRES|J7T1J9|QCand the second parameteriis the number of times that split operation is applied. This is the explanation of your regexSo, your regex is effectively equivalent to
asadsdas357902|in a way because the regex that comes after it is never tested. See thesplitmethod documentation here String#splitThis code would give you the same output