方法很简单,使用URL类的方法。
因为url是有几部分组成,所以先获得file内容,然后使用string类的split()方法分解字符串。
实例代码:
package mark.zhang;
import java.net.MalformedURLException;
import java.net.URL;
public class LastStr {
/**
* @param args
* @throws MalformedURLException
*/
public static void main(String[] args) {
String str = "http://blog.csdn.net/AndroidBluetooth";
String out = getLastString(str);
System.out.println("out content: " + out);
/*String file = url.getFile();
String protocol = url.getProtocol();
String host = url.getHost();
String userInfo = url.getUserInfo();
String path = url.getPath();
int defaultPort = url.getDefaultPort();
int port = url.getPort();
System.out.println("file: " + file);
System.out.println("protocol: " + protocol);
System.out.println("host: " + host);
System.out.println("userInfo: " + userInfo);
System.out.println("path: " + path);
System.out.println("defaultPort: " + defaultPort);
System.out.println("port: " + port);
String[] splitStr = file.split("/");
int len = splitStr.length;
System.out.println("" + splitStr[len-1]);*/
}
public static String getLastString(String str) {
URL url;
try {
url = new URL(str);
} catch (MalformedURLException e) {
return null;
}
String file = url.getFile();
String[] splitStr = file.split("/");
int len = splitStr.length;
String result = splitStr[len-1];
return result;
}
}