Tuesday, August 17, 2010

String Terminators

Courtesy of Sun Web Learning Center


1. class StringSplit {
2.   public static void main(String [] args) {
3.
4.     String s = "x1234 y56 z7 a";
5.     String [] sa = s.split("\\d");
6.     int count = 0;
7.     for( String x : sa)
8.       count++;
9.     System.out.println("total: " + count);
10.   }
11. }

What is the result?

A total: 3
B total: 4
C total: 7
D total: 8
E Compilation fails.
F An exception is thrown at runtime.

D is correct. The \d means that every digit is a terminator, which creates 7 entries, and then the end of the String creates the eighth terminator.

No comments:

Post a Comment