KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > xml > util > StringSplitter


1 package com.thaiopensource.xml.util;
2
3 public class StringSplitter {
4   private StringSplitter() {
5   }
6
7   public static String JavaDoc[] split(String JavaDoc str) {
8     int len = str.length();
9     int nTokens = 0;
10     for (int i = 0; i < len; i++)
11      if (!isSpace(str.charAt(i)) && (i == 0 || isSpace(str.charAt(i - 1))))
12        nTokens++;
13     String JavaDoc[] tokens = new String JavaDoc[nTokens];
14     nTokens = 0;
15     int tokenStart = -1;
16     for (int i = 0; i < len; i++) {
17       if (isSpace(str.charAt(i))) {
18         if (tokenStart >= 0) {
19           tokens[nTokens++] = str.substring(tokenStart, i);
20           tokenStart = -1;
21         }
22       }
23       else if (i == 0 || isSpace(str.charAt(i - 1)))
24        tokenStart = i;
25     }
26     if (tokenStart >= 0)
27       tokens[nTokens] = str.substring(tokenStart, len);
28     return tokens;
29   }
30
31   private static boolean isSpace(char c) {
32     switch (c) {
33     case ' ':
34     case '\r':
35     case '\n':
36     case '\t':
37       return true;
38     }
39     return false;
40   }
41
42 }
43
Popular Tags