问题:
在Java中使用 split()方法进行分割字符串时,遇到数组越界异常
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 0
String s = "E:\\file\\1.txt";
System.out.println(s.split(".")[1]) //取得后缀名
//解决方法:s.split("\\.")[1]
原因:
需要在 .
前面加上 转义字符 \\.
例如:
用 |
进行分割时,需输入 \\|
用 *
进行分割时,需输入 \\*
用 \\
进行分割时,需输入 \\\\