| Author |
SCJP6 study - regex string.split - strange results, can anyone explain please?
|
Nigel Shrin
Ranch Hand
Joined: May 18, 2009
Posts: 119
|
|
eg based on a whizlabs q:
The length of s is: 5
0
1 12
2
3
4 2
I was expecting even numbers of 1's ie 11 to disappear, so I would have 122 remaining.
It has removed 6 pairs of 11. How do you account for the way it is storing nulls?
>
|
Nigel
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Start with the original string...
"111211111121111"
Do a regex find to get the delimiters...
"11 12 11 11 11 2 11 11"
Add "X" to show the blank tokens...
"X 11 12 11 X 11 X 11 2 11 X 11 X "
Remove the trailing blanks, because you used the version of split() that removes them...
"X 11 12 11 X 11 X 11 2"
So, you get a final result of 5 tokens, which are blank, 12, blank, blank, and 2.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Ninad Kulkarni
Ranch Hand
Joined: Aug 31, 2007
Posts: 774
|
|
@Henry
Great explaination.
|
SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
|
 |
Nigel Shrin
Ranch Hand
Joined: May 18, 2009
Posts: 119
|
|
Hi Henry, I really appreciate the reply, the penny has dropped!
I've created a more complex example and managed to predict the result.
Cheers!
|
 |
 |
|
|
subject: SCJP6 study - regex string.split - strange results, can anyone explain please?
|
|
|