编写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