字符串拆分功能不返回所需的输出

时间:2015-12-09 08:52:49

标签: java split

我希望下面的课程中OUTPUT3

public class Pattern {
    public static void main(String[] args) {
        String data = "111,577,5099,541,142,
                       2015-08-01 00:08:42,2015-08-01 06:31:52|
                       674,898,7061,36,105,2015-08-01 19:28:45,
                       2015-08-02 14:46:27|948,522,1840,66,889,
                       2015-08-02 13:04:56,2015-08-02 19:39:57";

        if(data.contains("|"))
        {
           String pattern[] = data.split("|");
           System.out.println("the pattern length:  "+pattern.length);
        }
    }
}

输出:

  

图案长度:180

2 个答案:

答案 0 :(得分:4)

字符|是正则表达式中的特殊字符(split方法使用正则表达式)

你必须和

一起去
String pattern[] = data.split("\\|");

答案 1 :(得分:1)

同样更新你的代码,

 String pattern[] = data.split("\\|");