I want to write a function in java which removes the port if it is the default port.
So if given
String url80 = "http://www.somewhere.com:80/someplace";
it will return
String urlNo80 = "http://www.somewhere.com/someplace";
And if given
String url443 = "https://www.somewhere.com:443/someplace";
It will return
String urlNo443 = "https://www.somewhere.com/someplace";
Is there a better way to do it than
public String removePortIfDefault(String inUrl) {
String returnUrl = inUrl;
if (inUrl.contains("http://") && inUrl.contains(":80")) {
returnUrl = inUrl.replaceAll(":80", "");
}
if (inUrl.contains("https://") && inUrl.contains(":443")) {
returnUrl = inUrl.replaceAll(":443", "");
}
return returnUrl;
}