You've got already plenty of nice & working solutions written right here. I'm, however, kinda a fan of clean, easily-understandable code. There's nothing more wonderful than elegant one-command solution. Here you go. You're welcome!
public class Test {
public static String parseDomainName(String url) {
return (
url.startsWith("http://www.") ? url.replaceFirst("http://www\\.", "") :
url.startsWith("http://") ? url.replaceFirst("http://", "") :
url.startsWith("www.") ? url.replaceFirst("www\\.", "") :
url
);
}
public static void main(String[] args) {
System.out.println(parseDomainName("http://www.google.com"));
System.out.println(parseDomainName("http://google.com"));
System.out.println(parseDomainName("www.google.com"));
System.out.println(parseDomainName("google.com"));
System.out.println(parseDomainName("http://misleading.www.com"));
}
}
Alright, I'm just joking! But it's the single solution right here, which works in one command and doesn't use regular expressions (well, method replaceFirst() actually accepts only a regular expressions, but it'd be working on the same logic even with different method, which accepts only plain text string).
Use this solution as it's the best compromise if you really want to avoid using regular expressions. This solution I've made is really just a joke and it'd be horrible to see it used somewhere. :-)