如果我拆分「hello |」和「| hello」 with 「|」字符,然後我得到第一個值和第二個版本的兩個值。Java字符串拆分不一致
String[] arr1 = new String("hello|").split("\\|");
String[] arr2 = new String("|hello").split("\\|");
System.out.println("arr1 length: " + arr1.length + "\narr2 length: " + arr2.length);
此打印出:
ARR1長度:1
ARR2長度:2
這是爲什麼?
'split(String regex)' - *此方法的工作原理與通過調用具有給定表達式和極限參數爲零的雙參數拆分方法一樣。尾隨空字符串因此不包含在結果數組中。* –
檢查此問題http://stackoverflow.com/questions/15113272/java-split-by-a-successive-character/15113300#15113300 – m0skit0
還檢查[this]( http://stackoverflow.com/a/19503404/3841803) – silentprogrammer