Friday, July 1, 2011

Confusing split() with String

         I expect this program will really confuse you. Have tried and verified my results.
I was wrong @ 100%. Please have a try.

class StringSplit {
   
    public static void main(final String[] args) {

        final String s = "x1234 y56 z7 a";
        final String[] sa = s.split("\\d");
        int count = 0;
        for (final String x : sa) {
            count++;
        }
        System.out.println("total: " + count);
    }
}

I was concentrating on the regular expression. But the String.split() is the real culprit. 

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...