は上記の実証あなたのためのテスト・ケースのセットです:実行すると
public class Test {
public static void main(String[] args){
test("x,y");
test(",y");
test("");
test(",");
}
private static void test(String x){
System.out.println("testing split on value ["+x+"]");
String y = x.split(",")[0];
if(null == y){
System.out.println("x returned a null value for first array element");
} else if(y.length() < 1) {
System.out.println("x returned an empty string for first array element");
} else {
System.out.println("x returned a value for first array element");
}
}
}
、これはあなたが得るものです:
$ javac Test.java && java Test
testing split on value [x,y]
x returned a value for first array element
testing split on value [,y]
x returned an empty string for first array element
testing split on value []
x returned an empty string for first array element
testing split on value [,]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Test.test(Test.java:11)
at Test.main(Test.java:6)
それは単に書き込むことによって、行動のこの種をチェックするのは簡単ですこの事件を試みる小さなプログラムで、あなたが得るものを見てください – mfrankli
です。与えられた正規表現が含まれていなくても、常に最初の文字列を返します。 –
@mfrankli、ありがとう。それはほとんど役に立つ助言でした。明らかに私はテストプログラムを書くことができます(そして持っています)...私は考えていないか考えていた可能性のある異常値の状態について質問しています。 –