在Java中获取域名的相关部分

时间:2017-07-11 15:21:33

标签: java

如果我们有一个网址,例如www.google.de,我怎么才能获得" google"

在Java new URL (url).getHost();确实有效,但它给了我google.de 这不是我想要的。

谢谢

编辑:如果我们有类似www.google.co.uk的内容,那么我也希望只有" google"结果。

我不想要" google.de"或者" www.google"我只想要" google"

2 个答案:

答案 0 :(得分:1)

分割一段时间并选择第一个或第二个元素(取其中不是" www")将起作用:

URL url = new URL("http://www.host.ext.ext");
String host = url.getHost(); // host = "www.host.ext.ext"
String splitHost = host.split("\\.") // splitHost = { "www", "host", "ext", "ext" }

host = splitHost[0].equals("www") ? splitHost[1] : splitHost[0]; // host = "host"

如果之前有任何超过http://www.的内容,并且扩展程序可能超过两个"扩展程序" (例如.co.uk),那么没有简单的方法来获得你想要的部分。据我所知,您必须尝试迭代扩展列表并在最长匹配扩展名之前立即返回该部分。

答案 1 :(得分:0)

最基本的解决方案是使用

Public Class Trendline

#Region "Variables"
Private m_Slope As Decimal
Private m_Intercept As Decimal
Private m_Start As Decimal
Private m_End As Decimal
Private m_RSquared As Decimal
#End Region

#Region "Properties"
Public Property Slope() As Decimal
    Get
        Return m_Slope
    End Get
    Private Set
        m_Slope = Value
    End Set
End Property
Public Property Intercept() As Decimal
    Get
        Return m_Intercept
    End Get
    Private Set
        m_Intercept = Value
    End Set
End Property
Public Property Start() As Decimal
    Get
        Return m_Start
    End Get
    Private Set
        m_Start = Value
    End Set
End Property
Public Property [End]() As Decimal
    Get
        Return m_End
    End Get
    Private Set
        m_End = Value
    End Set
End Property
Public Property RSquared As Decimal
    Get
        Return m_RSquared
    End Get
    Set(value As Decimal)
        m_RSquared = value
    End Set
End Property
#End Region

#Region "New..."
Public Sub New(yAxisValues As IList(Of Decimal), xAxisValues As IList(Of Decimal))
    Me.New(yAxisValues.[Select](Function(t, i) New Tuple(Of Decimal, Decimal)(xAxisValues(i), t)))
End Sub
Public Sub New(data As IEnumerable(Of Tuple(Of [Decimal], [Decimal])))
    Dim cachedData = data.ToList()

    Dim n = cachedData.Count
    Dim sumX = cachedData.Sum(Function(x) x.Item1)
    Dim sumX2 = cachedData.Sum(Function(x) x.Item1 * x.Item1)
    Dim sumY = cachedData.Sum(Function(x) x.Item2)
    Dim sumY2 = cachedData.Sum(Function(x) x.Item2 * x.Item2)
    Dim sumXY = cachedData.Sum(Function(x) x.Item1 * x.Item2)

    'b = (sum(x*y) - sum(x)sum(y)/n)
    '      / (sum(x^2) - sum(x)^2/n)
    Slope = (sumXY - ((sumX * sumY) / n)) / (sumX2 - (sumX * sumX / n))

    'a = sum(y)/n - b(sum(x)/n)
    Intercept = (sumY / n) - (Slope * (sumX / n))

    '    r = (n * (Exy) - (Ex * Ey)) / (((n * (Ex2) - (Ex) ^ 2) ^ (1 / 2)) * ((n * (Ey2) - (Ey) ^ 2) ^ (1 / 2)))
    RSquared = ((n * (sumXY) - (sumX * sumY)) / (((n * (sumX2) - (sumX) ^ 2) ^ (1 / 2)) * ((n * (sumY2) - (sumY) ^ 2) ^ (1 / 2)))) ^ 2

    Start = GetYValue(cachedData.Min(Function(a) a.Item1))
    [End] = GetYValue(cachedData.Max(Function(a) a.Item1))
End Sub
#End Region

#Region "Methods / Functions"
Public Function GetYValue(xValue As Decimal) As Decimal
    Return Intercept + Slope * xValue
End Function
#End Region

End Class

或者你可以尝试这个https://stackoverflow.com/a/23079402/2555419

 System.out.println(url.split("\\.")[1]);