There is a task to trim a string with a hypertext link using Java tools.

For example from the line:

http://academy.it.ru/courses/?COUNT_3=1000000&arrFilter[CITY]=&arrFilter[PROPERTY_NAPR_CATALOG] 

or

 http://www.academy.it.ru/courses/?COUNT_3=1000000&arrFilter[CITY]=&arrFilter[PROPERTY_NAPR_CATALOG] 

need to make a string like:

 www.academy.it.ru 

I think I need to find // and the closing slash / . But how to implement this in the code for me is not clear. It is also not clear how to distinguish a single slash from a double one and from the rest of the slashes in the string.

Here is what I implemented:

 String findhtext=""; try{ XmlPullParser xpp = prepareXpp(); if (xpp.getText().contains("http://"||"https://")) findhtext=xpp.getText().substring(indexOf("//"),indexOf("/")) ; } 

But Android Studio swears that it cannot recognize the IndexOf method. I'm new to java.

What can be done to trim the link as required?

  • Well, first try str.indexOf where str is the string you are looking for. - pavel
  • findhtext = xpp.getText (). substring (xpp.indexOf ("//"), xpp.indexOf ("/")); \ nToo same. Error: (67, 66) error: cannot find symbol symbol indexOf (String) - Stariy Ded
  • xpp.getText().indexOf ... - pavel
  • findhtext = xpp.getText (). indexOf ("//"); Error: (68, 60) error: incompatible types: int cannot be converted to String - Stariy Ded
  • Well, so indexOf returns the position, and you apparently immediately put in the line. - pavel

1 answer 1

  1. android url get domain

  2. En-SO :

     import java.net.*; import java.io.*; public class ParseURL { public static void main(String[] args) throws Exception { URL aURL = new URL("http://example.com:80/docs/books/tutorial" + "/index.html?name=networking#DOWNLOADING"); System.out.println("protocol = " + aURL.getProtocol()); //http System.out.println("authority = " + aURL.getAuthority()); //example.com:80 System.out.println("host = " + aURL.getHost()); //example.com System.out.println("port = " + aURL.getPort()); //80 System.out.println("path = " + aURL.getPath()); // /docs/books/tutorial/index.html System.out.println("query = " + aURL.getQuery()); //name=networking System.out.println("filename = " + aURL.getFile()); ///docs/books/tutorial/index.html?name=networking System.out.println("ref = " + aURL.getRef()); //DOWNLOADING } } 
  • one
    Thank. I will try. I did not know that in Java there are tools for working with URIs. - Stariy Ded
  • one
    In Java, with a probability of 99% there is a means to work with something. - ReinRaus