Search code examples
javaandroidstringrss

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


I have two separate strings that are going to be fetched from an RSS Feed. These are my getLink() values.

String one would be like: http://www.example.in/something/source.rss

String two would be like: http://www.example.com/somthing/source2.rss

I only want to display on my ListView:

www.example.in

www.example.com

So, how do I split the string so that it ends at the third occurrence of "/" ?

Please help.Thank you.


Solution

  • try this code.

    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);
    

    Here output will be

    www.example.com

    Update

    efficient way to do it

    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();
    }