我有网址
http://www.facebook.com/post/ll.html
我想将网址分散到http://www.facebook.com/post/
和ll.html
请帮忙
答案 0 :(得分:3)
这样做的一种方法是:
String myStr = "http://www.facebook.com/post/ll.html";
String strEnd = myStr.substring(myStr.lastIndexOf('/')+1);
strEnd会有你想要的字符串。
答案 1 :(得分:1)
String x = "http://www.facebook.com/post/ll.html";
String[] splits = x.split("/");
String last = splits[splits.length - 1];
String first = x.substring(0, x.length() - last.length());
System.out.println(last); // 11.html
System.out.println(first); // http://www.facebook.com/post/
答案 2 :(得分:0)
试试这个:
if (null != str && str.length() > 0 )
{
int endIndex = str.lastIndexOf("/");
if (endIndex != -1)
{
String firststringurl = str.substring(0, endIndex);
String secondstringurl = str.substring(endIndex);
}
}
答案 3 :(得分:0)
我认为解决这个问题的最佳方法是使用the URL class,因为如果你只是做简单的字符串解析会有很多问题。以你的例子:
// Get ll.html
String filePart = url.getPath().substring(url.getPath().lastIndexOf('/')+1);
// Get /post/
String pathPart = url.getPath().substring(0, url.getPath().lastIndexOf('/')+1);
// Cut off full URL at end of first /post/
pathPart = url.toString().substring(0, url.toString().indexOf(pathPart)+pathPart.length());
这甚至可以处理http://www.facebook.com:80/ll.html/ll.html#foo/bar?wibble=1/ll.html
等网址。