java中split分割"."的问题

今天使用split分割"."的时候竟然失败了,通过百度发现原来要加转义字符才行。express

正确的写法:数组

String test="1.2.3";
String[] s1=test.split("\\.");

结果:函数

若是不加转义就会分割失败,返回一个空的字符串数组。this

API中是这样写的:spa

public String[] split(String regex)code

 Splits this string around matches of the given regular expression. blog

它的参数并非要分割的字符串,而是一个表达式,所以像*、+等都要加转义字符串

可是使用substring函数就没有这个问题string

例如取文件名的后缀能够这样写:it

String fileSuffix=fileName.substring(fileName.lastIndexOf(".")+1);
相关文章
相关标签/搜索