Skip to content

Java Program Project to Remove HTTP or HTTPS Protocol From URL

import java.net.URL;

public class test
{
    public static void main(String[] args)
    {
        try
        {
            // check if input is empty or not
            if (args.length == 0)
            {
                System.out.println("Invalid Input");
                return;
            }

            // check if input is a valid URL or not
            String input = args[0];
            URL url = new URL(input);
            String protocol = url.getProtocol();

            // get the domain.com/path
            String result = input.replaceFirst(protocol + ":", "");
            if (result.startsWith("//"))
            {
                result = result.substring(2);
            }

            System.out.println(result);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
String myString = "http://www.example.com";
//remove the http:// or https:// or www. from the string
myString.replace("http://","").replace("http:// www.","").replace("www.","");