1)思路一(已验证)
1、通过'\'关键字用split分割成数组
2、取分割后数组的最后一个就是文件名
另外,字符串中\是没意义的,需要2个\\
相关代码:
<script>
var a='C:\\Program Files\\Universal\\UFileUploaderD\\UFileUploaderD.dll';
var arr = a.split('\\');
alert(arr[arr.length-1]);
</script>
2)思路二(已验证)
package com.github;
import java.io.File;
/**
* @Description:
* @author: LeBlancs
* @CreateDate: 2016年12月23日
* @version: V1.0
*/
public class FileName
{
/**
* @param args
*/
public static void main(String[] args)
{
//举例:
String fNameA = " G:\\Java_Source\\navigation_tigra_menu\\demo1\\img\\lev1_arrow.gif ";
String fNameB = " /fnlab/statics/img/controller.png ";
//方法一:
File tempFile = new File(fNameA.trim());
String fileName = tempFile.getName();
System.out.println("fileName = " + fileName);
//方法二:
String fName1 = fNameA.trim();
String fName2 = fNameB.trim();
String fileName1 = fName1.substring(fName1.lastIndexOf("\\") + 1);
System.out.println("fileName = " + fileName1);
//或者
String fileName2 = fName2.substring(fName2.lastIndexOf("/") + 1);
System.out.println("fileName = " + fileName2);
//方法三:
String fName3 = fNameA.trim();
/** split里面必须是正则表达式,"\\"的作用是对字符串转义 */
String temp[] = fName3.split("\\\\");
String fileName3 = temp[temp.length - 1];
System.out.println("fileName = " + fileName3);
}
}