public class SubStrTest { public static void main(String[] args) { String str = "c:\\abc\\def\\text.txt"; System.out.println(str.substring(str.lastIndexOf("\\")+1)); } }
String[] split(String regex, int limit) Splits this string around matches of the given regular expression. 比如: import java.util.regex.*; public class Replacement{ public static void main(String[] args) throws Exception { Pattern p = Pattern.compile("[\]+"); //用Pattern的split()方法把字符串按"\"分割 String[] result = p.split( "c:\abc\def\text.txt"); for (int i=0; i<result.length; i++) System.out.println(result[i]); } }
int index = path.lastIndexOf('\');
String name = path.substring(index,path.length());
String[ ] sArr = s1.split("\");
out.println(sArr[ sArr.length() ]);
{
public static void main(String[] args)
{
String str = "c:\\abc\\def\\text.txt";
System.out.println(str.substring(str.lastIndexOf("\\")+1));
}
}
Splits this string around matches of the given regular expression. 比如:
import java.util.regex.*;
public class Replacement{
public static void main(String[] args) throws Exception {
Pattern p = Pattern.compile("[\]+");
//用Pattern的split()方法把字符串按"\"分割
String[] result = p.split( "c:\abc\def\text.txt");
for (int i=0; i<result.length; i++)
System.out.println(result[i]);
}
}
//这样分完所得的filename数组,
filename = {"c:","abc","del","text.txt"}filename[3] = "text.txt";
int index = path.lastIndexOf('\');
String name = path.substring(index,path.length());
这个应该是最好的。