• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

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

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?
  • 3
  • 3
1 Solution
Hello nahumba,

string1.Length > string2.Length

nahumbaAuthor Commented:
Hi martin,
This will not always work (I think), since the actual length does not always represents the IP address lentgh...
what do you mean ?
can you give some examples so I can be more strict in suggestions ?

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

nahumbaAuthor Commented:
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..
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
Check this article: http://www.codeguru.com/Csharp/.NET/net_general/internet/article.php/c10651/
this is in c# unfortunately but if you like the style, you can use this convertor for turning the code into vb.net

nahumbaAuthor Commented:
Thank you Martin - exactly what I've been looking for!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now