Get root domain from request

前端 未结 4 1711
青春惊慌失措
青春惊慌失措 2021-01-31 03:07

Consider the below URL as an example. http://www.test1.example.com

Is there any method by which I can get \"example.com\" as an output. I know there is

4条回答
  •  误落风尘
    2021-01-31 03:27

    You can do this as a simple String manipulation:

    String A = "http://www.test1.example.com";
    String B = A.substring(A.lastIndexOf('.', A.lastIndexOf('.')-1) + 1);
    

    There is no standard way obtain example.com from a.b.c.example.com because such a transformation is generally not useful. There are TLDs that don't allow registrations on the second level; for example all .uk domains. Given news.bbc.co.uk you'd want to end up with bbc.co.uk, right?

提交回复
热议问题