转载:http://androidstudy.iteye.com/blog/787560
方法一:利用String类
public String getFileName(String pathandname){
int start=pathandname.lastIndexOf("/");
int end=pathandname.lastIndexOf(".");
if (start!=-1 && end!=-1) {
return pathandname.substring(start+1, end);
}
else {
return null;
}
}
方法二:利用正则表达式
String regEx=".+\\\\(.+)$";
String str="C:\\Documents and Settings\\Administrator\\My Documents\\myfile.txt";
Pattern p=Pattern.compile(regEx);
Matcher m=p.matcher(str);
boolean rs=m.find();
if(rs){
for(int i=1;i<=m.groupCount();i++) {
System.out.println(m.group(i)); //括号内匹配内容
}
}