編寫Java程序來處理字符串中的null域--利用StringTokenizer分解字符串
先看一個場景分析:
import java.util.*;
public class StrTokDemo2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
StringTokenizer st = new StringTokenizer("Hello, World|of|Java", ", |");
while(st.hasMoreElements())
System.out.println("Token: " + st.nextElement());
}
}
結果:
Token: Hello
Token: World
Token: of
Token: Java
但如果使用這上面的程序讀取下面的行:
FirstName|Lastname|Company|PhoneNumber
可能會出現問題,例如有人沒有工作,他的Company一欄是空的(即形如:FirstName|Lastname| |PhoneNumber),也就是說兩個分隔符(空格和|)在一起,但當你運行這個程序時,卻沒有輸出“額外”的token(標記)。
我可以在構造StringTokenizer對象時增加一個參數True,表示希望將分隔符視為token,但這需要我在做近一步的處理,如下:
import java.util.*;
//使用StringTokenizer類,包括返回分隔符
public class StrTokDemo4 {
public final static int MAXFIELDS = 5;
public final static String DELIM = "|";
//處理一個字符串,返回一個各域組成的數組
public static String[] process(String line){
String[] results = new String[MAXFIELDS];
StringTokenizer st = new StringTokenizer(line, DELIM, true);
int i = 0;
//得到每一個StringTokenizer類
while(st.hasMoreTokens()){
String s = st.nextToken();
if(s.equals(DELIM)){
if(i++ >= MAXFIELDS)
throw new IllegalArgumentException("Input line " + line + " has too many fields");
continue;
}
results[i] = s;
}
return results;
}
public static void printResults(String input, String[] outputs){
System.out.println("Input: " + input);
for(int i = 0; i < outputs.length; i++)
System.out.println("Output " + i + " was: " + outputs[i]);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
printResults("A|B|C|D", process("A|B|C|D"));
printResults("A||C|D", process("A||C|D"));
printResults("A|||D|E", process("A|||D|E"));
}
}
結果:
Input: A|B|C|D
Output 0 was: A
Output 1 was: B
Output 2 was: C
Output 3 was: D
Output 4 was: null
Input: A||C|D
Output 0 was: A
Output 1 was: null
Output 2 was: C
Output 3 was: D
Output 4 was: null
Input: A|||D|E
Output 0 was: A
Output 1 was: null
Output 2 was: null
Output 3 was: D
Output 4 was: E