/** * @author lautturi.com * Java example: java transformar/cast string to an url *encoding a string to url */ import java.net.MalformedURLException; import java.net.URL; import java.util.*; public class Lautturi { public static void main(String[] args) throws MalformedURLException { String string = "https://www.lautturi.com/static/img/logo.png"; URL url; try { url = new URL(string); System.out.println(url.getHost()); System.out.println(url.getPath()); System.out.println(url.getPort()); System.out.println(url.toString()); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
output:
www.lautturi.com /static/img/logo.png -1 https://www.lautturi.com/static/img/logo.png