如何在字符第三次出现时拆分字符串? [android]

How do I split a string at the third occurrence of the character? [android]

我有两个单独的字符串,它们将从 RSS 提要中获取。这些是我的 getLink() 值。

第一个字符串是这样的:http://www.example.in/something/source.rss

字符串二将是:http://www.example.com/somthing/source2.rss

我只想在我的 ListView 上显示:

www.example.in

www.example.com

那么,如何拆分字符串以使其在第三次出现“/”时结束?

请help.Thank你

您的方法还可以,但效率不高,因为搜索条件很复杂...

使用 java.net 中的 URL class 而您实际上需要的是主机....

示例:

public static void main(String[] args) throws MalformedURLException {
       String a = "http://www.example.in/something/source.rss";
       String b = "http://www.example.com/somthing/source2.rss";
       System.out.println("host = " + new URL(a).getHost());
       System.out.println("host = " + new URL(b).getHost());
}

输出将是:

host = www.example.in

host = www.example.com

试试这个代码。

String kek = "http://www.example.com/somthing/source2.rss";
String ohh = kek.substring(7, kek.length());
String my = ohh.substring(0, ohh.indexOf("/"));
Log.e("test", my);

此处输出为

www.example.com

更新

有效的方法

String u = "http://www.example.com/somthing/source2.rss";
URL url = null;
try {
    url = new URL(u);
    String host = url.getHost();
    Log.e("test", host);
} catch (MalformedURLException e) {
    e.printStackTrace();
}

这很简单,也可以工作

    String str="http://www.example.in/something/source.rss";
    int init=-1;
    for(int i=0;i<3;i++){
        init=str.indexOf("/",init+1);
    }
    System.out.println(str.substring(0,init));