Created
January 25, 2013 03:03
-
-
Save nathanchen/4631387 to your computer and use it in GitHub Desktop.
StringTokenizer的用法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public int compare(String a, String b) { | |
| StringTokenizer aTokens = new StringTokenizer(a, "."); | |
| StringTokenizer bTokens = new StringTokenizer(b, "."); | |
| while (aTokens.hasMoreTokens()) { | |
| int aToken = Integer.parseInt(aTokens.nextToken()); | |
| System.out.println(aToken); | |
| if (bTokens.hasMoreTokens()) { | |
| int bToken = Integer.parseInt(bTokens.nextToken()); | |
| System.out.println(bToken); | |
| if (aToken != bToken) { | |
| return aToken < bToken ? -1 : 1; | |
| } | |
| } else { | |
| // a has some extra trailing tokens. if these are all zeroes, thats ok. | |
| if (aToken != 0) { | |
| return 1; | |
| } | |
| } | |
| } | |
| // b has some extra trailing tokens. if these are all zeroes, thats ok. | |
| while (bTokens.hasMoreTokens()) { | |
| if (Integer.parseInt(bTokens.nextToken()) != 0) | |
| return -1; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment