Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 仅获取URL部分_Java_Android - Fatal编程技术网

Java 仅获取URL部分

Java 仅获取URL部分,java,android,Java,Android,我有网址 http://www.facebook.com/post/ll.html 我想将url插入http://www.facebook.com/post/和ll.html 请帮助执行此操作的一种方法是: String x = "http://www.facebook.com/post/ll.html"; String[] splits = x.split("/"); String last = splits[splits.length - 1]; S

我有网址

http://www.facebook.com/post/ll.html

我想将url插入
http://www.facebook.com/post/
ll.html


请帮助执行此操作的一种方法是:

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/
String myStr = "http://www.facebook.com/post/ll.html";
String strEnd = myStr.substring(myStr.lastIndexOf('/')+1);
strEnd将拥有您想要的字符串。

试试这个:

if (null != str && str.length() > 0 )
    {
    int endIndex = str.lastIndexOf("/");
    if (endIndex != -1)  
    {
        String firststringurl = str.substring(0, endIndex); 
        String secondstringurl = str.substring(endIndex);
    }
    }  

我认为实现这一点的最好方法是同时使用,因为如果只进行简单的字符串解析,会有很多问题。例如:

// 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

使用Java的String.split()。