Java Program to Remove HTTP or HTTPS Protocol From URL Using Regular Expression

Java Program to Remove HTTP or HTTPS Protocol From URL Using Regular Expression

import java.net.URL;
 
public class test
{
    public static void main(String[] args)
    {
        try {
            URL url = new URL(args[0]);
            String protocol = url.getProtocol();
            String result = args[0].replaceFirst(protocol + ":", "");
            if (result.startsWith("//"))
            {
                result = result.substring(2);
            }
 
            System.out.println(result);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

String myString = "http://www.abc.com";
myString.replace("http://","").replace("http:// www.","").replace("www.","");

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment