0

我的问题是ms[ ]当我做 split() 时数组没有得到值;为什么会这样?

public class Test {

    public static void main(String[] args) {
        Date date = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss.");    //change format          
        String msgTime = ft.format(date);
        System.out.println(msgTime);

        String ms[] = msgTime.split(".");
        System.out.println(ms.length);
    }
} 
4

2 回答 2

1

问题是 split() 函数将正则表达式作为参数,而不是简单的字符串。和 ”。” 正则表达式表示“任何符号”。所以你只需要逃避它。

String ms[] = msgTime.split("\\.");

于 2013-04-05T17:55:46.463 回答
0

我猜你打算这样做

String ms[] = msgTime.split("\\.");

String.split()采用正则表达式,因此您应该转义任何特殊字符,例如..

于 2013-04-05T17:54:54.473 回答