IP address as string - determine if it is longer or short then the other -

Posted on 2007-08-06
Last Modified: 2013-11-26
Hi all,

I have two strings which represents an IP address.
I think it isn't possible, but I wanted to know, if there's a way to know if one string's length is longer / short then the other?
Question by:nahumba
    LVL 28

    Expert Comment

    Hello nahumba,

    string1.Length > string2.Length


    Author Comment

    Hi martin,
    This will not always work (I think), since the actual length does not always represents the IP address lentgh...
    LVL 28

    Expert Comment

    what do you mean ?
    can you give some examples so I can be more strict in suggestions ?


    Author Comment

    My application needs to scan a range of IPs from a given start address to an end address.

    I need to determine that the start address's length is shorter.. What I actually mean is, that the IP address length is determined by it's byte size (each octet is between 0 to 255) 32bits in total...

    So the string's length value just return the number of total characters in the string, I need to determine the IP address's byte size..
    LVL 9

    Expert Comment

    You can do something like this

       'Assumption IP is in valid IPV4 format
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim ip1 As String = ""
            Dim ip2 As String = ""
            Dim lngip1 As Long = getIPAsLong(ip1)
            Dim lngip2 As Long = getIPAsLong(ip2)

            If lngip1 > lngip2 Then
                MessageBox.Show("ip1 is larger then ip2")
            ElseIf lngip1 = lngip2 Then
                MessageBox.Show("ip1 is equal to  ip2")
            Else 'lngip1 < lngip2
                MessageBox.Show("ip1 is smaller than  ip2")
            End If
        End Sub

        Private Function getIPAsLong(ByVal str As String) As Long
            Dim sRes As String
            Dim i As Integer
            Dim aStr() As String
            aStr = str.Split(".")
            For i = 0 To aStr.Length - 1
                sRes &= paddingZero(aStr(i))
            Return CLng(sRes)

        End Function

        Private Function paddingZero(ByVal str As String) As String
            Dim res As String = str.Trim
            If (res.Length >= 3) Then
                res = res.Substring(0, 3)
                While (res.Length < 3)
                    res = "0" & res

                End While
            End If
            Return res
        End Function
    LVL 28

    Accepted Solution

    Check this article:
    this is in c# unfortunately but if you like the style, you can use this convertor for turning the code into


    Author Comment

    Thank you Martin - exactly what I've been looking for!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now