关于JAVA中split()函数遇到的一点问题
2012-12-19 20:36
561 查看
很简单的,就是想分割一个文件路径字符串,取它的扩展名。
String file=new String("c:\\test\\file.exe");
String[] temp = file.split(".");
程序运行到这里,就异常终止了。想想以前用split分割很多种符号,都没问题。随手试了试split("\\"),依然失败。又换了其他的各种符号":"、"##"等等却又没问题。难道"."和"\\"有什么不对么?
在仔细看看JDK的文档:
String[] java.lang.String.split(String regex)
split
public String[] split(String regex)
Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
The string "boo:and:foo", for example, yields the following results with these expressions:
Regex Result
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }
Parameters:
regex - the delimiting regular expression
Returns:
the array of strings computed by splitting this string around matches of the given regular expression
Throws:
PatternSyntaxException - if the regular expression's syntax is invalid
Since:
1.4
See Also:
Pattern
清清楚楚的写着参数是String regex,一个regular expression,正则表达式。哎,以前没认真看过,一直当成普通的字符串在用了,只是运气好,恰好没出错。知错就改:
改成String[] temp = file.split("\\.");或者String[] temp = file.split("[.]");就可以了。至于那个"\\"分割的话,用String[] temp = file.split("\\\\");就可以了。
String file=new String("c:\\test\\file.exe");
String[] temp = file.split(".");
程序运行到这里,就异常终止了。想想以前用split分割很多种符号,都没问题。随手试了试split("\\"),依然失败。又换了其他的各种符号":"、"##"等等却又没问题。难道"."和"\\"有什么不对么?
在仔细看看JDK的文档:
String[] java.lang.String.split(String regex)
split
public String[] split(String regex)
Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
The string "boo:and:foo", for example, yields the following results with these expressions:
Regex Result
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }
Parameters:
regex - the delimiting regular expression
Returns:
the array of strings computed by splitting this string around matches of the given regular expression
Throws:
PatternSyntaxException - if the regular expression's syntax is invalid
Since:
1.4
See Also:
Pattern
清清楚楚的写着参数是String regex,一个regular expression,正则表达式。哎,以前没认真看过,一直当成普通的字符串在用了,只是运气好,恰好没出错。知错就改:
改成String[] temp = file.split("\\.");或者String[] temp = file.split("[.]");就可以了。至于那个"\\"分割的话,用String[] temp = file.split("\\\\");就可以了。
相关文章推荐
- android编程中遇到的关于调试百度地图时出现的 java.lang.UnsatisfiedLinkError: initClass 问题的原因及解决办法
- java 关于split分割字符串,空结果不能得到的问题,多个分隔符的处理
- 关于javascript基于DWR调用java函数的传值问题
- 关于Java二进制和十六进制转化的一点问题
- 基于vbox虚拟机Centos6.5的关于Oracle 11.2g单例安装遇到的一点问题
- 关于ORACLE通过file_id与block_id定位数据库对象遇到的问题的一点思考
- 关于BaiduMapSDK遇到的问题的一点总结
- 关于在ubuntu10.04版本(而且该版本安装了OPEN JDK时),安装SUN JAVA遇到了几个问题并且安装成功
- 关于java回调函数问题
- 关于java中String类型的split方法问题
- java 中在Lab2_实验 中遇到的问题 关于equals hashcode 和==
- 关于ActivityGroup使用过程中遇到的一点问题
- java关于split分割字符串,空的字符串不能得到的问题
- 关于ActivityGroup使用过程中遇到的一点问题
- 关于在做java的Web开发中遇到跨域访问的问题的解决方法汇总
- java关于split分割字符串,空的字符串不能得到的问题
- 关于java中多线程的问题的一点思考(什么情况下需要考虑到线程,以及线程安全的问题?)
- 关于我遇到的java中编译没错运行出错的一些问题(不定期更新)
- 遇到一个关于静态成员函数的问题
- php关于使用iconv(...)函数对字符进行中文转换时,遇到的一个问题