I have the URL
http://www.facebook.com/post/ll.html
I want to spilt the url into http://www.facebook.com/post/
and ll.html
Please help
One way of doing this is:
String myStr = "http://www.facebook.com/post/ll.html";
String strEnd = myStr.substring(myStr.lastIndexOf('/')+1);
strEnd will have the string you desire.
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/
Try this:
if (null != str && str.length() > 0 )
{
int endIndex = str.lastIndexOf("/");
if (endIndex != -1)
{
String firststringurl = str.substring(0, endIndex);
String secondstringurl = str.substring(endIndex);
}
}
I think the best way to approach this is to also use the URL class , as there are a lot of gotchas if you just do simple string parsing. For your example:
// 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());
This will even cope with URLs like http://www.facebook.com:80/ll.html/ll.html#foo/bar?wibble=1/ll.html
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.